Repository navigation
Print each PyHealth log line once in Jupyter and Colab - #1285
Merged
Merged
Conversation
The pyhealth logger has its own stdout handler and also propagated to the root logger. Jupyter, Colab and logging.basicConfig give the root logger a handler, so every message printed twice. Stop propagation from the pyhealth logger; users who want PyHealth messages routed through their own root configuration can remove pyhealth.handler and turn propagation back on. Adds tests, a FAQ entry and examples/logging_setup.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why
pyhealth/__init__.pygives thepyhealthlogger its own stdout handler, and the logger also propagates to the root logger. Jupyter, Colab andlogging.basicConfig()all give the root logger a handler, so in those settings every PyHealth message prints twice: dataset loading, task processing, the Trainer's epoch summaries. It makes notebooks noticeably harder to read (it showed up throughout the Colab runs for #1281).What
propagate = Falseon thepyhealthlogger, next to where PyHealth adds its handler, with a comment showing how to opt back in.logging.basicConfigwould still print twice. Turning off propagation is deterministic and matches how most libraries that print by default behave.INFOlevel, the stdout handler, child loggers (pyhealth.trainer,pyhealth.datasets, ...) still reach PyHealth's handler, andTrainer'slog.txtfile logging is unaffected.docs/faq.rst. Example:examples/logging_setup.py(no data needed) shows the default, a quieter level, and routing through root.Once this is merged, the
logging.getLogger("pyhealth").propagate = Falseline in the tutorials' install cell (#1281) can be dropped.Tests
tests/core/test_logging_setup.py(synthetic, no data):pyhealth.*logger is printed once by PyHealth's handler and not by the root handler (fails on master, passes here);INFOis still printed andDEBUGis not;Trainer'sset_loggerstill writeslog.txt, and the message prints once.Also ran the modules that use
assertLogson PyHealth loggers and the Trainer-based tests (test_early_stopping,test_model_mode,test_xgboost_model,test_nested_processor_truncation,test_default_cache_dir): 45 tests OK.tools/check_pr_rules.pypasses.🤖 Generated with Claude Code