File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -413,6 +413,8 @@ class LogRecord:
413
413
sinfo : str | None = ...,
414
414
) -> None : ...
415
415
def getMessage (self ) -> str : ...
416
+ # Allows setting contextual information on LogRecord objects as per the docs, see #7833
417
+ def __setattr__ (self , __name : str , __value : Any ) -> None : ...
416
418
417
419
_L = TypeVar ("_L" , bound = Logger | LoggerAdapter [Any ])
418
420
Original file line number Diff line number Diff line change
1
+ import logging
2
+ from typing import Any
3
+
4
+ # This pattern comes from the logging docs, and should therefore pass a type checker
5
+ # See https://docs.python.org/3/library/logging.html#logrecord-objects
6
+
7
+ old_factory = logging .getLogRecordFactory ()
8
+
9
+
10
+ def record_factory (* args : Any , ** kwargs : Any ) -> logging .LogRecord :
11
+ record = old_factory (* args , ** kwargs )
12
+ record .custom_attribute = 0xDECAFBAD
13
+ return record
14
+
15
+
16
+ logging .setLogRecordFactory (record_factory )
You can’t perform that action at this time.
0 commit comments