Skip to content

fix(logging): bind the memory logger's stream at bootstrap - #166

Merged
lesnik512 merged 1 commit into
mainfrom
fix/log-stream-bind-at-bootstrap
Aug 10, 2026
Merged

fix(logging): bind the memory logger's stream at bootstrap#166
lesnik512 merged 1 commit into
mainfrom
fix/log-stream-bind-at-bootstrap

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Implements planning/changes/2026-08-10.02-log-stream-bind-at-bootstrap.md, filed in #163.

_MemoryLoggerFactoryConfig.log_stream defaulted to a bare sys.stdout, evaluated once at module import. Every MemoryLoggerFactory handler therefore wrote to whatever sys.stdout was 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. A default_factory moves the lookup to bootstrap, where the root handler already binds.

Reproduced with a plain LoggingInstrument under contextlib.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.stdout between import and bootstrap.

Promoted into architecture/instruments.md; added to planning/releases/1.4.0.md (written, not yet tagged); change-file summary finalized.

One consequence recorded in the change file: capsys can 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 the litestar logger anyway — more precise, and independent of pytest's capture mode.

🤖 Generated with Claude Code

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.
@lesnik512
lesnik512 merged commit cfb7650 into main Aug 10, 2026
11 checks passed
@lesnik512
lesnik512 deleted the fix/log-stream-bind-at-bootstrap branch August 10, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant