File tree Expand file tree Collapse file tree 2 files changed +60
-2
lines changed
Expand file tree Collapse file tree 2 files changed +60
-2
lines changed Original file line number Diff line number Diff line change 1818from structlog .typing import WrappedLogger
1919
2020
21- __version__ = "0.7 "
21+ __version__ = "0.8 "
2222
2323
2424class 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
Original file line number Diff line number Diff line change 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+ ]
You can’t perform that action at this time.
0 commit comments