Skip to content

Map the exception method name to ERROR severity#156

Open
jeremyk wants to merge 1 commit into
multani:mainfrom
jeremyk:fix/exception-method-severity
Open

Map the exception method name to ERROR severity#156
jeremyk wants to merge 1 commit into
multani:mainfrom
jeremyk:fix/exception-method-severity

Conversation

@jeremyk

@jeremyk jeremyk commented Jun 30, 2026

Copy link
Copy Markdown

structlog.stdlib.BoundLogger.exception() logs with the method name "exception", which isn't in SEVERITY_MAPPING — so logger.exception() falls back to DEFAULT severity instead of ERROR. The default BoundLogger is unaffected (its .exception() uses "error").

Adds "exception": "ERROR" to the mapping and a test.

Example of logger.exception() before:
Screenshot 2026-06-29 at 9 36 56 PM

Fixes: #155

structlog.stdlib.BoundLogger.exception() logs with the method name
"exception", which isn't in SEVERITY_MAPPING, so it fell back to the
DEFAULT severity. The default BoundLogger is unaffected (it uses "error").
Comment thread tests/test_log.py
Comment on lines +68 to +75
def test_exception_method_maps_to_error_severity() -> None:
"""`structlog.stdlib.BoundLogger.exception()` logs with the "exception"
method name, unlike the default ``BoundLogger`` (which uses "error"). It
must still map to ERROR severity.
"""
event_dict = LogSeverity()(None, "exception", {CLOUD_LOGGING_KEY: {}})

assert event_dict[CLOUD_LOGGING_KEY]["severity"] == "ERROR"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change the test to:

  1. Get a logger bound to a stdlib logger
  2. Call the stdlib-bound logger with an exception (cf. the test just below)
  3. Assert the output like the other test

You can add the "logger bound with a stdlib logger" as a new fixture into conftests.py ; I suspect it might be useful for other tests as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logger.exception() maps to DEFAULT severity instead of ERROR

2 participants