Repository navigation
fix: remove FastStream's stderr handler while lite-bootstrap logs FastStream records - #346
Merged
Merged
Conversation
…tStream records FastStream puts a StreamHandler(sys.stderr) on the faststream logger at import time, and the MemoryHandler lite-bootstrap adds sat next to it, so every FastStream record was written twice. Take the existing handlers off while bootstrapped and put them back on teardown. Fixes #336.
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.
Fixes #336.
At import time, FastStream puts a
StreamHandler(sys.stderr)with its own text format on thefaststreamlogger.FastStreamLoggingInstrumenthands brokersstructlog.get_logger("faststream"), and lite-bootstrap's logger factory then adds itsMemoryHandlerto that same stdlib logger without removing FastStream's handler. As a result, every FastStream record was written twice: once as lite-bootstrap JSON on stdout and once in FastStream's format on stderr.With this change,
FastStreamLoggingInstrument.bootstrapsaves the handlers already on thefaststreamlogger and clears them before lite-bootstrap's handler goes on.teardown()puts them back after the parent teardown has closed lite-bootstrap's handler, so a torn-down process logs the way it did before bootstrap. Nothing changes when the application has no brokers, because the instrument leaves the logger alone in that case.The issue also raised clearing existing handlers in
MemoryLoggerFactory.__call__for every logger it creates. I didn't do that, because it would reach into loggers lite-bootstrap does not own.Test, written first and failing on main:
test_faststream_logging_replaces_faststream_stderr_handler: while bootstrapped, thefaststreamlogger has no handler writing tosys.stderrand has exactly oneMemoryHandler. After teardown, its handlers are the ones it had before. The test doesn't compare the full handler list, because pytest attaches its own capture handlers to that logger.Measured with
benchmarks/frameworkson macOS (Apple M2, CPython 3.14) against a local Redis, in-process, per consumed message. Both columns use the same venv with only this file swapped, over 3 interleaved runs of 5 repeats:log(defaultfaststream_log_level=WARNING, nothing logged)log_info(faststream_log_level=logging.INFO, 2 records per message)That saves about 12 µs per message, or 6 µs per record. #336's Linux prototype measured 10 µs. The
+35.2FastStream row and the paragraph after the consume table inbenchmarks/README.mddescribe main at the commit they were measured on. I left them as they are, as #345 did for its Litestar rows, until the next Linux run.Checked locally:
tyare clean, all 393 tests pass, and coverage is 100%.floorssteps for the FastStream target pass on 3.11 and 3.14 (faststream 0.6.0).Behaviour change: FastStream's colourized lines on stderr stop while lite-bootstrap is bootstrapped with brokers. The same records still come out as lite-bootstrap JSON on stdout. The
logging_unset_handlers=["faststream"]workaround is no longer needed, and it still works. No config changes.