From 399b3f8229889630106ddbe98c990962b4ce70a3 Mon Sep 17 00:00:00 2001 From: Lev Vereshchagin Date: Tue, 7 Jul 2026 14:02:21 +0300 Subject: [PATCH] Add extra processors for foreign logging --- .../instruments/logging_instrument.py | 6 +++- tests/instruments/test_logging.py | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/microbootstrap/instruments/logging_instrument.py b/microbootstrap/instruments/logging_instrument.py index 2d72c09..8be5994 100644 --- a/microbootstrap/instruments/logging_instrument.py +++ b/microbootstrap/instruments/logging_instrument.py @@ -220,7 +220,11 @@ def _configure_foreign_loggers(self) -> None: ) if self.instrument_config.service_debug else structlog.stdlib.ProcessorFormatter( - foreign_pre_chain=[*STRUCTLOG_PRE_CHAIN_PROCESSORS, self._timestamper_processor], + foreign_pre_chain=[ + *STRUCTLOG_PRE_CHAIN_PROCESSORS, + self._timestamper_processor, + *self.instrument_config.logging_extra_processors, + ], processors=[ structlog.stdlib.ProcessorFormatter.remove_processors_meta, STRUCTLOG_FORMATTER_PROCESSOR, diff --git a/tests/instruments/test_logging.py b/tests/instruments/test_logging.py index 0e988d9..0550afa 100644 --- a/tests/instruments/test_logging.py +++ b/tests/instruments/test_logging.py @@ -12,6 +12,7 @@ from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor +from structlog.typing import EventDict, WrappedLogger from microbootstrap import LoggingConfig from microbootstrap.bootstrappers.fastapi import FastApiBootstrapper, FastApiLoggingInstrument @@ -196,6 +197,36 @@ def test_fastapi_logging_bootstrap_ignores_health( class TestForeignLogs: + def test_extra_processors_apply_to_foreign_loggers_in_non_debug_mode( + self, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + def add_extra_field(_: WrappedLogger, __: str, event_dict: EventDict) -> EventDict: + event_dict["processor_marker"] = "from-extra-processor" + return event_dict + + root_logger = logging.getLogger() + monkeypatch.setattr(root_logger, "handlers", []) + foreign_logger = logging.getLogger("foreign-extra-processors") + monkeypatch.setattr(foreign_logger, "handlers", []) + monkeypatch.setattr(foreign_logger, "propagate", True) + + logging_instrument = LoggingInstrument( + LoggingConfig( + service_debug=False, + logging_buffer_capacity=0, + logging_extra_processors=[add_extra_field], + ) + ) + logging_instrument.bootstrap() + + foreign_logger.info("said hi") + + stdout = capsys.readouterr().out + assert '"event":"said hi"' in stdout + assert '"processor_marker":"from-extra-processor"' in stdout + def test_litestar(self, capsys: pytest.CaptureFixture[str]) -> None: logger = logging.getLogger()