Skip to content

Commit 9ef7f47

Browse files
authored
Merge pull request #31 from b0g3r/issue30
Add test for #30
2 parents e77f947 + 73226c3 commit 9ef7f47

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

pytest_structlog/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from structlog.typing import WrappedLogger
1919

2020

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

2323

2424
class EventList(List[EventDict]):
@@ -74,7 +74,7 @@ def process(
7474
self, logger: WrappedLogger, method_name: str, event_dict: EventDict
7575
) -> EventDict:
7676
"""Captures a logging event, appending it as a dict in the event list."""
77-
event_dict["level"] = method_name
77+
event_dict["level"] = method_name if method_name != "exception" else "error"
7878
self.events.append(event_dict)
7979
raise structlog.DropEvent
8080

tests/test_issue30.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import logging.config
2+
3+
import pytest
4+
import structlog
5+
6+
from pytest_structlog import StructuredLogCapture
7+
8+
9+
@pytest.fixture
10+
def stdlib_bound_logger_configure():
11+
"""
12+
From the original structlog issue: https://github.com/hynek/structlog/issues/584#issue-2063338394
13+
"""
14+
structlog.configure(
15+
processors=[
16+
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
17+
],
18+
logger_factory=structlog.stdlib.LoggerFactory(),
19+
wrapper_class=structlog.stdlib.BoundLogger,
20+
)
21+
22+
logging_config = {
23+
"version": 1,
24+
"formatters": {
25+
"json": {
26+
"()": structlog.stdlib.ProcessorFormatter,
27+
"processors": [
28+
structlog.processors.add_log_level,
29+
structlog.processors.JSONRenderer(),
30+
],
31+
}
32+
},
33+
"handlers": {
34+
"json": {
35+
"class": "logging.StreamHandler",
36+
"formatter": "json",
37+
},
38+
},
39+
"root": {
40+
"handlers": ["json"],
41+
},
42+
}
43+
logging.config.dictConfig(logging_config)
44+
45+
46+
def log_exception():
47+
logger = structlog.get_logger()
48+
try:
49+
1 / 0
50+
except ZeroDivisionError:
51+
logger.exception("event_name")
52+
53+
54+
def test_exception_level(stdlib_bound_logger_configure, log: StructuredLogCapture):
55+
log_exception()
56+
assert log.events == [
57+
{"event": "event_name", "exc_info": True, "level": "error"},
58+
]

0 commit comments

Comments
 (0)