Skip to content

Map the exception method name to ERROR severity - #156

Merged
multani merged 3 commits into
multani:mainfrom
jeremyk:fix/exception-method-severity
Jul 7, 2026
Merged

Map the exception method name to ERROR severity#156
multani merged 3 commits into
multani:mainfrom
jeremyk:fix/exception-method-severity

Conversation

@jeremyk

@jeremyk jeremyk commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

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 Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I updated the PR and this avoids needing a new test but still catches this missing config line. Let me know if this works.

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.

Thanks, looks good 👍

jeremyk and others added 2 commits July 6, 2026 15:09
Parametrize `test_exception` so a real `.exception()` call runs through the full pipeline and both method names are asserted to produce identical ERROR output — reusing the existing assertion, no new fixture or duplicated payload.

@multani multani left a comment

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.

Thanks for the fix!

@multani
multani merged commit 86b311d into multani:main Jul 7, 2026
7 checks passed
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