fix(logging): bind the memory logger's stream at bootstrap - #166
Merged
Conversation
log_stream defaulted to a bare sys.stdout, evaluated once when the module was imported, so a process that rebound sys.stdout before bootstrapping kept logging structlog output to the stale stream while the root-logger handler installed at bootstrap followed the new one. Resolve it through a default_factory so both agree. Affects every bootstrapper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
planning/changes/2026-08-10.02-log-stream-bind-at-bootstrap.md, filed in #163._MemoryLoggerFactoryConfig.log_streamdefaulted to a baresys.stdout, evaluated once at module import. EveryMemoryLoggerFactoryhandler therefore wrote to whateversys.stdoutwas at import time, even when the process rebound it before bootstrapping — while_configure_foreign_loggers' root-logger handler, created at bootstrap, followed the new one. The two paths disagreed. Adefault_factorymoves the lookup to bootstrap, where the root handler already binds.Reproduced with a plain
LoggingInstrumentundercontextlib.redirect_stdout: before the fix the buffer stayed empty while the line went to the real stdout. That is the new test, written failing first.Affects every bootstrapper, not just Litestar. Behavior is unchanged when nothing rebinds
sys.stdoutbetween import and bootstrap.Promoted into
architecture/instruments.md; added toplanning/releases/1.4.0.md(written, not yet tagged); change-file summary finalized.One consequence recorded in the change file:
capsyscan now observe structlog output in a test that bootstraps inside the test body, which it could not before. The Litestar access-logging tests keep recording through a handler on thelitestarlogger anyway — more precise, and independent of pytest's capture mode.🤖 Generated with Claude Code