Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing from snowplow_tracker.emitters adds a handler to the root logger #373

Open
w-arnott opened this issue Jan 14, 2025 · 0 comments
Open

Comments

@w-arnott
Copy link

w-arnott commented Jan 14, 2025

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.

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

No branches or pull requests

1 participant