-
Notifications
You must be signed in to change notification settings - Fork 69
Description
Describe the bug
When importing things from snowplow_tracker.emitters, the module calls logging.basicConfig() which adds a StreamHandler to the Python root logger.
This interferes with applications which set up their own handlers as messages will be logged by both the StreamHandler and the custom handlers.
To Reproduce
The following script should work to illustrate the issue. The root logger has no handlers before importing Emitter, but has a StreamHandler afterwards. Adding another handler to the root logger, and then logging a message from a child logger results in the message being logged to the console twice: once with the default format set up by basicConfig and once with the format specified for formatter.
import logging
root_logger = logging.getLogger()
print(root_logger.hasHandlers()) # False
from snowplow_tracker.emitters import Emitter
print(root_logger.hasHandlers()) # True
print(root_logger.handlers) # [<StreamHandler <stderr> (NOTSET)>]
# Create a handler with custom formatting and add it to the root logger
handler = logging.StreamHandler()
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
root_logger.addHandler(handler)
# Create a child logger and log a message
module_logger = logging.getLogger(__name__)
module_logger.warning("Test Message.")
# WARNING:__main__:Test Message.
# __main__ - WARNING - Test Message.Expected behavior
Importing snowplow_tracker does not modify the root logger. The logger created in emitters.py will propagate its messages to the root logger. At that point, it's the user's responsibility to add handlers that meet their application's use cases so the call to logging.basicConfig in emitters.py should be unnecessary.
Environment (please complete the following information):
- OS: macOS 12.7.6
- Python Version: 3.11.9
- Version: 1.0.4
Additional context
The bug in the example above appears to be caused by line 40 in emitters.py. However, there are other files in the repo which would cause the same bug if their contents were imported, see search results here.