Skip to content

Commit

Permalink
stdlib: add structlog.stdlib.ProcessorFormatter(use_get_message=True)
Browse files Browse the repository at this point in the history
Fixes #520
  • Loading branch information
hynek committed Sep 8, 2023
1 parent 2a6b4c0 commit 2d847e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
- `structlog.dev.RichTracebackFormatter` that allows to configure the traceback formatting.
[#542](https://github.com/hynek/structlog/issues/542)

- stdlib: `ProcessorFormatter` can now be told to now render the log record message using `getMessage` and just `str(record.msg)` instead.
[#550](https://github.com/hynek/structlog/issues/550)


### Fixed

Expand Down
11 changes: 10 additions & 1 deletion src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ class ProcessorFormatter(logging.Formatter):
This parameter exists for historic reasons. Please use *processors*
instead.
use_get_message:
If True, use ``record.getMessage`` to get a fully rendered log
message, otherwise use ``str(record.msg)``. (default: True)
Raises:
TypeError: If both or neither *processor* and *processors* are passed.
Expand All @@ -963,6 +967,7 @@ class ProcessorFormatter(logging.Formatter):
.. deprecated:: 21.3.0
*processor* (singular) in favor of *processors* (plural). Removal is not
planned.
.. versionadded:: 23.2.0 *use_get_message*
"""

def __init__(
Expand All @@ -974,6 +979,7 @@ def __init__(
keep_stack_info: bool = False,
logger: logging.Logger | None = None,
pass_foreign_args: bool = False,
use_get_message: bool = True,
*args: Any,
**kwargs: Any,
) -> None:
Expand All @@ -998,6 +1004,7 @@ def __init__(
self.keep_stack_info = keep_stack_info
self.logger = logger
self.pass_foreign_args = pass_foreign_args
self.use_get_message = use_get_message

def format(self, record: logging.LogRecord) -> str:
"""
Expand Down Expand Up @@ -1029,7 +1036,9 @@ def format(self, record: logging.LogRecord) -> str:
logger = self.logger
meth_name = record.levelname.lower()
ed = {
"event": record.getMessage(),
"event": record.getMessage()
if self.use_get_message
else str(record.msg),
"_record": record,
"_from_structlog": False,
}
Expand Down

0 comments on commit 2d847e6

Please sign in to comment.