MCPcopy Create free account
hub / github.com/hynek/structlog / ExtraAdder

Class ExtraAdder

src/structlog/stdlib.py:885–942  ·  view source on GitHub ↗

Add extra attributes of `logging.LogRecord` objects to the event dictionary. This processor can be used for adding data passed in the ``extra`` parameter of the `logging` module's log methods to the event dictionary. Args: allow: An optional collection of a

Source from the content-addressed store, hash-verified

883
884
885class ExtraAdder:
886 """
887 Add extra attributes of `logging.LogRecord` objects to the event
888 dictionary.
889
890 This processor can be used for adding data passed in the ``extra``
891 parameter of the `logging` module's log methods to the event dictionary.
892
893 Args:
894 allow:
895 An optional collection of attributes that, if present in
896 `logging.LogRecord` objects, will be copied to event dictionaries.
897
898 If ``allow`` is None all attributes of `logging.LogRecord` objects
899 that do not exist on a standard `logging.LogRecord` object will be
900 copied to event dictionaries.
901
902 .. versionadded:: 21.5.0
903 """
904
905 __slots__ = ("_copier",)
906
907 def __init__(self, allow: Collection[str] | None = None) -> None:
908 self._copier: Callable[[EventDict, logging.LogRecord], None]
909 if allow is not None:
910 # The contents of allow is copied to a new list so that changes to
911 # the list passed into the constructor does not change the
912 # behaviour of this processor.
913 self._copier = functools.partial(self._copy_allowed, [*allow])
914 else:
915 self._copier = self._copy_all
916
917 def __call__(
918 self, logger: logging.Logger, name: str, event_dict: EventDict
919 ) -> EventDict:
920 record: logging.LogRecord | None = event_dict.get("_record")
921 if record is not None:
922 self._copier(event_dict, record)
923 return event_dict
924
925 @classmethod
926 def _copy_all(
927 cls, event_dict: EventDict, record: logging.LogRecord
928 ) -> None:
929 for key, value in record.__dict__.items():
930 if key not in _LOG_RECORD_KEYS:
931 event_dict[key] = value
932
933 @classmethod
934 def _copy_allowed(
935 cls,
936 allow: Collection[str],
937 event_dict: EventDict,
938 record: logging.LogRecord,
939 ) -> None:
940 for key in allow:
941 if key in record.__dict__:
942 event_dict[key] = record.__dict__[key]

Callers 3

test_add_extraMethod · 0.90
test_no_recordMethod · 0.90
test_add_extra_e2eMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_add_extraMethod · 0.72
test_no_recordMethod · 0.72
test_add_extra_e2eMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…