Skip to content

Move logging.basicConfig() out of the library into the CLI entry points - #275

Open
abeeranajam31 wants to merge 1 commit into
Unbabel:masterfrom
abeeranajam31:fix/259-no-basicconfig-in-library-init
Open

Move logging.basicConfig() out of the library into the CLI entry points#275
abeeranajam31 wants to merge 1 commit into
Unbabel:masterfrom
abeeranajam31:fix/259-no-basicconfig-in-library-init

Conversation

@abeeranajam31

Copy link
Copy Markdown

Closes #259.

Root cause

comet/__init__.py calls logging.basicConfig(level=logging.INFO, format="%(message)s") at import time. basicConfig() configures the root logger for the whole Python process the instant comet (or anything that imports it, like evaluate.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-time basicConfig(INFO) locks that in immediately, so unless a user's code happens to setLevel(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 logging behavior, so it doesn't need a comet install to check:

# 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

The fix

comet/cli/score.py already has a --quiet flag that loops over every registered logger and force-sets it to ERROR — 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 moved logging.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.py and mbr.py didn't import logging at all (they were relying on comet/__init__.py's side effect), so I added it to both.

All 5 changed files pass py_compile.

…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>
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.

Suppress PyTorch Lightning debug output

1 participant