Skip to content

Commit

Permalink
Add __setattr__ to logging.LogRecord (#8064)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jun 12, 2022
1 parent 8bb18aa commit 1aa5663
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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)

0 comments on commit 1aa5663

Please sign in to comment.