Skip to content

Stop configuring the root logger on import - #102

Open
joeyleake wants to merge 1 commit into
meshcore-dev:mainfrom
joeyleake:fix/58-dont-configure-root-logger
Open

Stop configuring the root logger on import#102
joeyleake wants to merge 1 commit into
meshcore-dev:mainfrom
joeyleake:fix/58-dont-configure-root-logger

Conversation

@joeyleake

Copy link
Copy Markdown

Fixes #58

Problem

Importing meshcore_py called logging.basicConfig(level=logging.INFO) at
module 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__ always
called logger.setLevel(...) even when neither debug nor only_error
was passed, forcing the level to INFO and overriding whatever the
embedding app had already configured.

Fix

  • src/meshcore/__init__.py: removed the basicConfig() call. The module
    logger now just gets a NullHandler, so the library stays silent by
    default without emitting "no handlers found" warnings, and never touches
    the root logger.

  • src/meshcore/meshcore.py: MeshCore.__init__ now only calls
    logger.setLevel(...) when the caller explicitly passes debug=True or
    only_error=True. The default case (neither flag set) leaves whatever
    level the embedding app configured alone, instead of forcing INFO.

  • README.md: added a short note after the Debug Mode section showing the
    idiomatic pattern for consumers who want meshcore's logs:

    import logging
    logging.basicConfig(level=logging.INFO)
    logging.getLogger("meshcore").setLevel(logging.DEBUG)

Confirmed the logger name exported from __init__.py's __all__ isn't
referenced anywhere else in the package or examples, so removing
basicConfig doesn't change what that export points to for existing users
who import it directly.

Testing

Checked tests/ for anything depending on the implicit basicConfig (grep
for basicConfig/getLogger/caplog) — the only hits are
caplog.at_level(...) calls in test_reader.py, which set their own level
independently 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.

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

root logger gets configured for any app that imports this library

1 participant