snowplow / snowplow/snowplow-python-tracker
Importing from snowplow_tracker.emitters adds a handler to the root logger
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 46
- Forks
- 65
- Avg merge
- 5h 45m
- Merged PRs (30d)
- 1
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at line 40 of snowplow_tracker/emitters.py, then inspect the other files identified by the repository search for logging.basicConfig. Verify that importing these modules no longer modifies the root logger while the emitters logger still propagates messages, using the issue's reproduction script to confirm that applications do not receive duplicate output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100