Skip to content

Commit

Permalink
Merge pull request #33 from wimglenn/tb
Browse files Browse the repository at this point in the history
keep tb renderers. closes #32
  • Loading branch information
wimglenn authored May 30, 2024
2 parents 193bcfc + 566933f commit d862294
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pytest_structlog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from structlog.typing import WrappedLogger


__version__ = "0.8"
__version__ = "0.9"


class EventList(List[EventDict]):
Expand Down Expand Up @@ -140,6 +140,12 @@ def log(
# if there was a positional argument formatter in there, keep it
# see https://github.com/wimglenn/pytest-structlog/issues/18
new_processors.append(processor)
elif processor is structlog.processors.format_exc_info:
# traceback render
# see https://github.com/wimglenn/pytest-structlog/issues/32
new_processors.append(processor)
elif processor is getattr(structlog.processors, "dict_tracebacks", object()):
new_processors.append(processor)
elif processor is merge_contextvars:
# if merging contextvars, preserve
# see https://github.com/wimglenn/pytest-structlog/issues/20
Expand Down
51 changes: 51 additions & 0 deletions tests/test_issue32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
import structlog


@pytest.fixture
def stdlib_bound_logger_configure():
structlog.configure(
processors=[
structlog.processors.format_exc_info,
structlog.processors.JSONRenderer(),
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
)


@pytest.fixture
def stdlib_bound_logger_configure_dict_tb():
structlog.configure(
processors=[
structlog.processors.dict_tracebacks,
structlog.processors.JSONRenderer(),
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
)


def log_exception():
logger = structlog.get_logger()
try:
1 / 0
except ZeroDivisionError:
logger.exception("event_name")


def test_exception_traceback(stdlib_bound_logger_configure, log):
log_exception()
[event] = log.events
err = event["exception"]
assert err.startswith("Traceback")
assert "ZeroDivisionError" in err


def test_exception_dict_traceback(stdlib_bound_logger_configure_dict_tb, log):
log_exception()
[event] = log.events
[err] = event["exception"]
assert isinstance(err, dict)
assert err["exc_type"] == "ZeroDivisionError"
assert err["exc_value"] == "division by zero"

0 comments on commit d862294

Please sign in to comment.