Skip to content
Merged
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
14 changes: 11 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ def mock_logger_env() -> Generator[None, None, None]:


@pytest.fixture
def logger(mock_logger_env: None) -> Generator[WrappedLogger, None, None]:
"""Setup a logger for testing and return it"""
def logger(
request: pytest.FixtureRequest, mock_logger_env: None
) -> Generator[WrappedLogger, None, None]:
"""Setup a logger for testing and return it.

Indirectly parametrize with a ``wrapper_class`` (e.g.
``structlog.stdlib.BoundLogger``) to bind a non-default logger.
"""

structlog.reset_defaults()
structlog.contextvars.clear_contextvars()

processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)
structlog.configure(
processors=processors, wrapper_class=getattr(request, "param", None)
)
logger = structlog.get_logger()

yield logger
Expand Down
9 changes: 9 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from unittest.mock import patch

import pytest
import structlog
from _pytest.capture import CaptureFixture
from structlog.typing import WrappedLogger
Expand Down Expand Up @@ -28,7 +29,15 @@ def test_normal(stdout: T_stdout, logger: WrappedLogger) -> None:
assert msg == expected


@pytest.mark.parametrize(
"logger",
[None, structlog.stdlib.BoundLogger],
indirect=True,
ids=["default", "stdlib"],
)
def test_exception(stdout: T_stdout, logger: WrappedLogger) -> None:
# The stdlib logger's exception() logs with the "exception" method name
# (the default one uses "error"); both must map to ERROR severity.
try:
1 / 0
except ZeroDivisionError:
Expand Down