Skip to content

"exception" is a possible log level from processors.add_log_level since 23.3.0 #584

@kimsappi

Description

@kimsappi

structlog.processors.add_log_level used to report the level of "exception" events as "error", but since 23.3.0 with certain configurations it's possible to get "exception" instead:

$ pip3 install structlog==23.2.0 && python3 main.py
{'exc_info': True, 'event': 'test', '_record': <LogRecord: root, 40, 223, "{...}">, '_from_structlog': True, 'level': 'error'}

$ pip3 install structlog==23.3.0 && python3 main.py
{'exc_info': True, 'event': 'test', '_record': <LogRecord: root, 40, 217, "{...}">, '_from_structlog': True, 'level': 'exception'}

I wonder if this is intentional, since it's not very clearly listed in the changelog. There is "stdlib: structlog.stdlib.BoundLogger.exception()'s handling ofLogRecord.exc_info is now set consistent with logging." but I don't think this change falls into that category.

It seems to be reliant on wrapper_class=structlog.stdlib.BoundLogger.

Example code:

import logging.config
import structlog

structlog.configure(
    processors=[
        structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
    ],
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
)

logging_config = {
    "version": 1,
    "formatters": {
        "json": {
            "()": structlog.stdlib.ProcessorFormatter,
            "processors": [
                structlog.processors.add_log_level,
            ],
        }
    },
    "handlers": {
        "json": {
            "class": "logging.StreamHandler",
            "formatter": "json",
        },
    },
    "root": {
        "handlers": ["json"],
    },
}
logging.config.dictConfig(logging_config)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions