Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion microbootstrap/instruments/logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions tests/instruments/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
Loading