Stop configuring the root logger on import - #102
Open
joeyleake wants to merge 1 commit into
Open
Conversation
logging.basicConfig() at import time in __init__.py silently clobbered the embedding application's own logging setup. Replace it with a NullHandler so the library stays silent by default without touching global config. Also stop MeshCore.__init__ from unconditionally forcing the "meshcore" logger to INFO when neither debug= nor only_error= is passed - only set a level when the caller explicitly asks for one, otherwise leave whatever the app already configured alone. Document the idiomatic logging setup for consumers in README.md. Fixes meshcore-dev#58
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 #58
Problem
Importing meshcore_py called
logging.basicConfig(level=logging.INFO)atmodule load time, which configures Python's root logger for any
application that imports the library — silently overriding that
application's own logging setup. Separately,
MeshCore.__init__alwayscalled
logger.setLevel(...)even when neitherdebugnoronly_errorwas passed, forcing the level to
INFOand overriding whatever theembedding app had already configured.
Fix
src/meshcore/__init__.py: removed thebasicConfig()call. The modulelogger now just gets a
NullHandler, so the library stays silent bydefault without emitting "no handlers found" warnings, and never touches
the root logger.
src/meshcore/meshcore.py:MeshCore.__init__now only callslogger.setLevel(...)when the caller explicitly passesdebug=Trueoronly_error=True. The default case (neither flag set) leaves whateverlevel the embedding app configured alone, instead of forcing
INFO.README.md: added a short note after the Debug Mode section showing theidiomatic pattern for consumers who want meshcore's logs:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("meshcore").setLevel(logging.DEBUG)
Confirmed the
loggername exported from__init__.py's__all__isn'treferenced anywhere else in the package or examples, so removing
basicConfigdoesn't change what that export points to for existing userswho import it directly.
Testing
Checked tests/ for anything depending on the implicit
basicConfig(grepfor
basicConfig/getLogger/caplog) — the only hits arecaplog.at_level(...)calls in test_reader.py, which set their own levelindependently and needed no changes.
Full suite: 179 passed, 2 failed (test_send_cmd,
test_advert_path_preserves_embedded_zero_bytes) — both pre-existing on a
clean main checkout, unrelated to this change.