From 200ec859c770070e8fa6e8eee9608d1ddc280c2a Mon Sep 17 00:00:00 2001 From: Jeremy Kallman Date: Tue, 30 Jun 2026 12:49:42 -0700 Subject: [PATCH] Map the exception method name to ERROR severity 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"). --- structlog_gcp/constants.py | 1 + tests/test_log.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/structlog_gcp/constants.py b/structlog_gcp/constants.py index 13df942..44d8bf6 100644 --- a/structlog_gcp/constants.py +++ b/structlog_gcp/constants.py @@ -16,6 +16,7 @@ "warn": "WARNING", # Warning events might cause problems. "warning": "WARNING", # Warning events might cause problems. "error": "ERROR", # Error events are likely to cause problems. + "exception": "ERROR", # structlog.stdlib.BoundLogger.exception() method name. "critical": "CRITICAL", # Critical events cause more severe problems or outages. # "alert": "ALERT", # A person must take an action immediately. # "emergency": "EMERGENCY", # One or more systems are unusable. diff --git a/tests/test_log.py b/tests/test_log.py index 8ff8229..e522104 100644 --- a/tests/test_log.py +++ b/tests/test_log.py @@ -6,6 +6,8 @@ from structlog.typing import WrappedLogger import structlog_gcp +from structlog_gcp.constants import CLOUD_LOGGING_KEY +from structlog_gcp.processors import LogSeverity from .conftest import T_stdout @@ -63,6 +65,16 @@ def test_exception(stdout: T_stdout, logger: WrappedLogger) -> None: assert msg == expected +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" + + def test_service_context_default(stdout: T_stdout, logger: WrappedLogger) -> None: try: 1 / 0