diff --git a/pytest_structlog/__init__.py b/pytest_structlog/__init__.py index b639963..1012ff6 100644 --- a/pytest_structlog/__init__.py +++ b/pytest_structlog/__init__.py @@ -18,7 +18,7 @@ from structlog.typing import WrappedLogger -__version__ = "0.8" +__version__ = "0.9" class EventList(List[EventDict]): @@ -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 diff --git a/tests/test_issue32.py b/tests/test_issue32.py new file mode 100644 index 0000000..03a72e5 --- /dev/null +++ b/tests/test_issue32.py @@ -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"