import logging from typing import Any # The list contains all the attributes listed in # http://docs.python.org/library/logging.html#logrecord-attributes RESERVED_FIELDS = ( "args", "asctime", "created", "exc_info", "exc_text", "filename", "funcName", "id", "levelname", "levelno", "lineno", "module", "msecs", "msecs", "message", "msg", "name", "pathname", "process", "processName", "relativeCreated", "thread", "threadName", "extra", "auth_token", "password", "stack_info", "taskName", ) def get_extra_fields( record: logging.LogRecord, reserved: tuple[str, ...] = RESERVED_FIELDS ) -> dict[str, Any]: easy_types = (str, bool, dict, float, int, list, type(None)) fields: dict[str, Any] = {} for key, value in record.__dict__.items(): if key not in reserved: if isinstance(value, easy_types): fields[key] = value else: fields[key] = repr(value) return fields