1
- from __future__ import annotations
1
+ from future import annotations
2
2
import logging
3
3
import logging .handlers
4
4
import time
@@ -71,18 +71,29 @@ def tracer_injection(_: WrappedLogger, __: str, event_dict: EventDict) -> EventD
71
71
return event_dict
72
72
73
73
74
- DEFAULT_STRUCTLOG_PROCESSORS : typing .Final [list [typing .Any ]] = [
75
- structlog .stdlib .filter_by_level ,
76
- structlog .stdlib .add_log_level ,
74
+ DEFAULT_STRUCTLOG_PRE_CHAIN_PROCESSORS : typing .Final [list [typing .Any ]] = [
77
75
structlog .stdlib .add_logger_name ,
78
- tracer_injection ,
76
+ structlog . stdlib . add_log_level ,
79
77
structlog .stdlib .PositionalArgumentsFormatter (),
80
78
structlog .processors .TimeStamper (fmt = "%Y-%m-%d %H:%M:%S" ),
81
79
structlog .processors .StackInfoRenderer (),
82
80
structlog .processors .format_exc_info ,
81
+ ]
82
+ DEFAULT_STRUCTLOG_PROCESSORS : typing .Final [list [typing .Any ]] = [
83
+ * DEFAULT_STRUCTLOG_PRE_CHAIN_PROCESSORS ,
84
+ structlog .stdlib .filter_by_level ,
85
+ tracer_injection ,
83
86
structlog .processors .UnicodeDecoder (),
84
87
]
85
- DEFAULT_STRUCTLOG_FORMATTER_PROCESSOR : typing .Final = structlog .processors .JSONRenderer (serializer = orjson .dumps )
88
+
89
+
90
+ def _serialize_log_with_orjson_to_string (value : typing .Any , ** kwargs : typing .Any ) -> str : # noqa: ANN401
91
+ return orjson .dumps (value , ** kwargs ).decode ()
92
+
93
+
94
+ DEFAULT_STRUCTLOG_FORMATTER_PROCESSOR : typing .Final = structlog .processors .JSONRenderer (
95
+ serializer = _serialize_log_with_orjson_to_string
96
+ )
86
97
87
98
88
99
class MemoryLoggerFactory (structlog .stdlib .LoggerFactory ):
@@ -150,6 +161,8 @@ def bootstrap(self) -> None:
150
161
for unset_handlers_logger in self .instrument_config .logging_unset_handlers :
151
162
logging .getLogger (unset_handlers_logger ).handlers = []
152
163
164
+ stream_handler : typing .Final = logging .StreamHandler ()
165
+ root_logger : typing .Final = logging .getLogger ()
153
166
structlog .configure (
154
167
processors = [
155
168
* DEFAULT_STRUCTLOG_PROCESSORS ,
@@ -165,6 +178,17 @@ def bootstrap(self) -> None:
165
178
wrapper_class = structlog .stdlib .BoundLogger ,
166
179
cache_logger_on_first_use = True ,
167
180
)
181
+ stream_handler .setFormatter (
182
+ structlog .stdlib .ProcessorFormatter (
183
+ foreign_pre_chain = [
184
+ * DEFAULT_STRUCTLOG_PRE_CHAIN_PROCESSORS ,
185
+ structlog .stdlib .ProcessorFormatter .remove_processors_meta ,
186
+ ],
187
+ processors = [structlog .processors .JSONRenderer ()],
188
+ logger = root_logger ,
189
+ )
190
+ )
191
+ root_logger .addHandler (stream_handler )
168
192
169
193
@classmethod
170
194
def get_config_type (cls ) -> type [LoggingConfig ]:
0 commit comments