Skip to content

Commit e25705b

Browse files
authored
Make foreign logs pretty in debug mode (#132)
* Enable logging in debug mode and improve log message format * Fix logging instrument processor configuration for debug mode * Fix logging instrument configuration logic
1 parent 0092250 commit e25705b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

microbootstrap/instruments/logging_instrument.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def fill_log_message(
4747
http_version: typing.Final = request.scope["http_version"]
4848
log_on_correct_level: typing.Final = getattr(access_logger, log_level)
4949
log_on_correct_level(
50+
f"{http_method} {url_with_query}",
5051
http={
5152
"url": url_with_query,
5253
"status_code": status_code,
@@ -146,10 +147,10 @@ def remove_trailing_slashes_from_logging_exclude_endpoints(self) -> typing_exten
146147

147148
class LoggingInstrument(Instrument[LoggingConfig]):
148149
instrument_name = "Logging"
149-
ready_condition = "Works only in non-debug mode"
150+
ready_condition = "Always ready"
150151

151152
def is_ready(self) -> bool:
152-
return not self.instrument_config.service_debug
153+
return True
153154

154155
def teardown(self) -> None:
155156
structlog.reset_defaults()
@@ -159,6 +160,8 @@ def _unset_handlers(self) -> None:
159160
logging.getLogger(unset_handlers_logger).handlers = []
160161

161162
def _configure_structlog_loggers(self) -> None:
163+
if self.instrument_config.service_debug:
164+
return
162165
structlog.configure(
163166
processors=[
164167
structlog.stdlib.filter_by_level,
@@ -181,6 +184,15 @@ def _configure_foreign_loggers(self) -> None:
181184
stream_handler: typing.Final = logging.StreamHandler(sys.stdout)
182185
stream_handler.setFormatter(
183186
structlog.stdlib.ProcessorFormatter(
187+
foreign_pre_chain=structlog.get_config()["processors"][:-1],
188+
processors=[
189+
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
190+
structlog.get_config()["processors"][-1],
191+
],
192+
logger=root_logger,
193+
)
194+
if self.instrument_config.service_debug
195+
else structlog.stdlib.ProcessorFormatter(
184196
foreign_pre_chain=STRUCTLOG_PRE_CHAIN_PROCESSORS,
185197
processors=[
186198
structlog.stdlib.ProcessorFormatter.remove_processors_meta,

0 commit comments

Comments
 (0)