Move logging.basicConfig() out of the library into the CLI entry points - #275
Open
abeeranajam31 wants to merge 1 commit into
Open
Move logging.basicConfig() out of the library into the CLI entry points#275abeeranajam31 wants to merge 1 commit into
abeeranajam31 wants to merge 1 commit into
Conversation
…ts (fixes Unbabel#259) comet/__init__.py called logging.basicConfig(level=logging.INFO, ...) at import time. basicConfig() configures the *root* logger for the whole process the moment comet is imported, which is a well-known anti-pattern for libraries (Python's own logging docs: "libraries should not configure logging"). It's also the actual reason the workarounds in Unbabel#259 don't reliably suppress PyTorch Lightning's output: any third-party logger without its own explicit level set (most of PL's) inherits INFO from the root logger config that comet already locked in, and simply gets missed if the user doesn't happen to set every PL logger name individually. Verified the mechanism directly (no comet install needed, this is plain stdlib logging behavior): # current comet behavior: basicConfig(INFO) at "import" time >>> logging.basicConfig(level=logging.INFO, format='%(message)s') >>> logging.getLogger('pytorch_lightning.accelerators.cuda').setLevel(logging.ERROR) # user silences the logger they know about >>> logging.getLogger('pytorch_lightning.utilities.rank_zero').info('GPU available: True (cuda), used: True') # a different PL logger they didn't GPU available: True (cuda), used: True <- still leaks # fixed: no basicConfig call anywhere >>> logging.getLogger('pytorch_lightning.utilities.rank_zero').info('GPU available: True (cuda), used: True') (nothing printed - suppressed by Python's default WARNING+ threshold) comet/cli/score.py already had a --quiet flag that loops over every registered logger and sets it to ERROR - i.e. the CLI already treats "everything at INFO by default" as the CLI's own choice, not the library's. This moves logging.basicConfig() into each of the four CLI entry points (score_command, train_command, compare_command, mbr_command) instead of deleting it, so `comet-score`/`comet-train`/ `comet-compare`/`comet-mbr` keep their exact current default output; only library usage (`import comet`, `evaluate.load("comet")`) changes, letting the calling application's own logging config take over as Python's docs recommend. compare.py and mbr.py didn't import logging at all (they were relying on comet/__init__.py's side effect), so I added the import in both. Co-Authored-By: Claude Sonnet 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.
Closes #259.
Root cause
comet/__init__.pycallslogging.basicConfig(level=logging.INFO, format="%(message)s")at import time.basicConfig()configures the root logger for the whole Python process the instantcomet(or anything that imports it, likeevaluate.load("comet")) is imported — this is the exact anti-pattern Python's own logging docs warn against for libraries.It's also the concrete reason the workarounds in #259 don't fully work: PyTorch Lightning's various loggers (
pytorch_lightning.*/lightning.pytorch.*, the exact names vary by PL version) mostly have no explicit level of their own, so they inherit whatever level the root logger ends up at.comet's import-timebasicConfig(INFO)locks that in immediately, so unless a user's code happens tosetLevel(ERROR)on the exact right logger name for their installed PL version, some messages leak through — which is exactly what both commenters on #259 ran into (each found a slightly different, incomplete set of logger names to silence).Verification
This is plain stdlib
loggingbehavior, so it doesn't need acometinstall to check:The fix
comet/cli/score.pyalready has a--quietflag that loops over every registered logger and force-sets it toERROR— i.e. "verbose INFO logging by default" is already treated as the CLI's choice, not something the importable library should impose on every caller.So rather than deleting the call outright (which would silently change
comet-score's default output), I movedlogging.basicConfig(level=logging.INFO, format="%(message)s")into each of the four CLI entry points (score_command,train_command,compare_command,mbr_command). The CLIs keep their exact current default behavior; only library usage changes, letting the calling application configure logging itself, as intended.compare.pyandmbr.pydidn'timport loggingat all (they were relying oncomet/__init__.py's side effect), so I added it to both.All 5 changed files pass
py_compile.