Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions microbootstrap/instruments/logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def tracer_injection(_: WrappedLogger, __: str, event_dict: EventDict) -> EventD
structlog.stdlib.add_logger_name,
tracer_injection,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
Expand Down Expand Up @@ -149,6 +148,9 @@ class LoggingConfig(BaseInstrumentConfig):
)
logging_exclude_endpoints: list[str] = pydantic.Field(default_factory=lambda: ["/health/", "/metrics"])
logging_turn_off_middleware: bool = False
logging_timestamper_extra_params: dict[str, typing.Any] = pydantic.Field(
default_factory=lambda: {"fmt": "%Y-%m-%d %H:%M:%S"}
)

@pydantic.model_validator(mode="after")
def remove_trailing_slashes_from_logging_exclude_endpoints(self) -> typing_extensions.Self:
Expand All @@ -172,6 +174,10 @@ def _unset_handlers(self) -> None:
for unset_handlers_logger in self.instrument_config.logging_unset_handlers:
logging.getLogger(unset_handlers_logger).handlers = []

@property
def _timestamper_processor(self) -> structlog.processors.TimeStamper:
return structlog.processors.TimeStamper(**self.instrument_config.logging_timestamper_extra_params)

def _configure_structlog_loggers(self) -> None:
if self.instrument_config.service_debug:
structlog.configure(
Expand All @@ -186,6 +192,7 @@ def _configure_structlog_loggers(self) -> None:
processors=[
structlog.stdlib.filter_by_level,
*STRUCTLOG_PRE_CHAIN_PROCESSORS,
self._timestamper_processor,
*self.instrument_config.logging_extra_processors,
STRUCTLOG_FORMATTER_PROCESSOR,
],
Expand Down Expand Up @@ -213,7 +220,7 @@ def _configure_foreign_loggers(self) -> None:
)
if self.instrument_config.service_debug
else structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=STRUCTLOG_PRE_CHAIN_PROCESSORS,
foreign_pre_chain=[*STRUCTLOG_PRE_CHAIN_PROCESSORS, self._timestamper_processor],
processors=[
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
STRUCTLOG_FORMATTER_PROCESSOR,
Expand Down
Loading