mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-09-14 09:46:39 +00:00
Stop configuring the root logger on import
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 #58
This commit is contained in:
@@ -371,6 +371,15 @@ meshcore = await MeshCore.create_serial("/dev/ttyUSB0", debug=True)
|
||||
|
||||
This logs detailed information about commands sent and events received.
|
||||
|
||||
meshcore_py does not configure Python's root logger or call `logging.basicConfig()` itself - it only attaches a `NullHandler` so it stays silent by default. To see its logs, configure logging in your own application, e.g.:
|
||||
|
||||
```python
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.getLogger("meshcore").setLevel(logging.DEBUG)
|
||||
```
|
||||
|
||||
## Common Examples
|
||||
|
||||
### Sending Messages to Contacts
|
||||
|
||||
@@ -9,9 +9,11 @@ from .packets import BinaryReqType
|
||||
from .serial_cx import SerialConnection
|
||||
from .tcp_cx import TCPConnection
|
||||
|
||||
# Setup default logger
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
# Setup default logger. Libraries must not configure the root logger (that's
|
||||
# the embedding application's call) - a NullHandler just silences the "no
|
||||
# handlers found" warning when the app hasn't configured logging at all.
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.addHandler(logging.NullHandler())
|
||||
|
||||
__all__ = [
|
||||
"BinaryReqType",
|
||||
|
||||
@@ -46,13 +46,13 @@ class MeshCore:
|
||||
self.commands = CommandHandler(default_timeout=default_timeout)
|
||||
self.commands.set_contact_getter_by_prefix(self.get_contact_by_key_prefix)
|
||||
|
||||
# Set up logger
|
||||
# Set up logger. Only override the level when the caller explicitly
|
||||
# asked for debug/only_error behavior - otherwise leave whatever
|
||||
# level the embedding app already configured alone.
|
||||
if debug:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
elif only_error:
|
||||
logger.setLevel(logging.ERROR)
|
||||
else:
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
# Set up connections
|
||||
self.commands.set_connection(self.connection_manager)
|
||||
|
||||
Reference in New Issue
Block a user