Skip to content

Add __setattr__ to logging.LogRecord #8064

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

Merged
merged 2 commits into from
Jun 12, 2022
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
2 changes: 2 additions & 0 deletions stdlib/logging/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ class LogRecord:
sinfo: str | None = ...,
) -> None: ...
def getMessage(self) -> str: ...
# Allows setting contextual information on LogRecord objects as per the docs, see #7833
def __setattr__(self, __name: str, __value: Any) -> None: ...

_L = TypeVar("_L", bound=Logger | LoggerAdapter[Any])

Expand Down
16 changes: 16 additions & 0 deletions test_cases/stdlib/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging
from typing import Any

# This pattern comes from the logging docs, and should therefore pass a type checker
# See https://docs.python.org/3/library/logging.html#logrecord-objects

old_factory = logging.getLogRecordFactory()


def record_factory(*args: Any, **kwargs: Any) -> logging.LogRecord:
record = old_factory(*args, **kwargs)
record.custom_attribute = 0xDECAFBAD
return record


logging.setLogRecordFactory(record_factory)