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
| 883 | |
| 884 | |
| 885 | class 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] |
no outgoing calls
searching dependent graphs…