Skip to content

Commit d862294

Browse files
authored
Merge pull request #33 from wimglenn/tb
keep tb renderers. closes #32
2 parents 193bcfc + 566933f commit d862294

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

pytest_structlog/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from structlog.typing import WrappedLogger
1919

2020

21-
__version__ = "0.8"
21+
__version__ = "0.9"
2222

2323

2424
class EventList(List[EventDict]):
@@ -140,6 +140,12 @@ def log(
140140
# if there was a positional argument formatter in there, keep it
141141
# see https://github.com/wimglenn/pytest-structlog/issues/18
142142
new_processors.append(processor)
143+
elif processor is structlog.processors.format_exc_info:
144+
# traceback render
145+
# see https://github.com/wimglenn/pytest-structlog/issues/32
146+
new_processors.append(processor)
147+
elif processor is getattr(structlog.processors, "dict_tracebacks", object()):
148+
new_processors.append(processor)
143149
elif processor is merge_contextvars:
144150
# if merging contextvars, preserve
145151
# see https://github.com/wimglenn/pytest-structlog/issues/20

tests/test_issue32.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
import structlog
3+
4+
5+
@pytest.fixture
6+
def stdlib_bound_logger_configure():
7+
structlog.configure(
8+
processors=[
9+
structlog.processors.format_exc_info,
10+
structlog.processors.JSONRenderer(),
11+
],
12+
logger_factory=structlog.stdlib.LoggerFactory(),
13+
wrapper_class=structlog.stdlib.BoundLogger,
14+
)
15+
16+
17+
@pytest.fixture
18+
def stdlib_bound_logger_configure_dict_tb():
19+
structlog.configure(
20+
processors=[
21+
structlog.processors.dict_tracebacks,
22+
structlog.processors.JSONRenderer(),
23+
],
24+
logger_factory=structlog.stdlib.LoggerFactory(),
25+
wrapper_class=structlog.stdlib.BoundLogger,
26+
)
27+
28+
29+
def log_exception():
30+
logger = structlog.get_logger()
31+
try:
32+
1 / 0
33+
except ZeroDivisionError:
34+
logger.exception("event_name")
35+
36+
37+
def test_exception_traceback(stdlib_bound_logger_configure, log):
38+
log_exception()
39+
[event] = log.events
40+
err = event["exception"]
41+
assert err.startswith("Traceback")
42+
assert "ZeroDivisionError" in err
43+
44+
45+
def test_exception_dict_traceback(stdlib_bound_logger_configure_dict_tb, log):
46+
log_exception()
47+
[event] = log.events
48+
[err] = event["exception"]
49+
assert isinstance(err, dict)
50+
assert err["exc_type"] == "ZeroDivisionError"
51+
assert err["exc_value"] == "division by zero"

0 commit comments

Comments
 (0)