Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add structlog.stdlib.ProcessorFormatter(use_get_message=True) #550

Merged
merged 9 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

- `structlog.threadlocal.tmp_bind()` now also works with `BoundLoggerLazyProxy` (in other words: before anything is bound to a bound logger).

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

- stdlib: `structlog.stdlib.BoundLogger.exception()`'s handling of`LogRecord.exc_info` is now set consistent with `logging`.
[#571](https://github.com/hynek/structlog/issues/571)
[#572](https://github.com/hynek/structlog/issues/572)
Expand Down
11 changes: 10 additions & 1 deletion src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,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 @@ -976,6 +980,7 @@ class ProcessorFormatter(logging.Formatter):
.. deprecated:: 21.3.0
*processor* (singular) in favor of *processors* (plural). Removal is not
planned.
.. versionadded:: 23.3.0 *use_get_message*
"""

def __init__(
Expand All @@ -987,6 +992,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 @@ -1011,6 +1017,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 @@ -1043,7 +1050,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
28 changes: 28 additions & 0 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,34 @@ def emit(self, record):

assert not records

def test_use_get_message_false(self):
"""
If use_get_message_is False, the event is obtained using
str(record.msg) instead of calling record.getMessage. That means
positional formatting is not performed.
"""
event_dicts = []

def capture(_, __, ed):
event_dicts.append(ed.copy())

return str(ed)

proc = ProcessorFormatter(processors=[capture], use_get_message=False)

record = logging.LogRecord(
"foo",
logging.INFO,
"path.py",
42,
"le msg: %s",
("keep separate",),
None,
)

assert proc.format(record)
assert "le msg: %s" == event_dicts[0]["event"]


@pytest_asyncio.fixture(name="abl")
async def _abl(cl):
Expand Down