Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions structlog_gcp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Comment on lines +68 to +75

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.



def test_service_context_default(stdout: T_stdout, logger: WrappedLogger) -> None:
try:
1 / 0
Expand Down