You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importloggingroot_logger=logging.getLogger()
print(root_logger.hasHandlers()) # Falsefromsnowplow_tracker.emittersimportEmitterprint(root_logger.hasHandlers()) # Trueprint(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 messagemodule_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.
The text was updated successfully, but these errors were encountered:
Describe the bug
When importing things from
snowplow_tracker.emitters
, the module callslogging.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 aStreamHandler
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 bybasicConfig
and once with the format specified forformatter
.Expected behavior
Importing snowplow_tracker does not modify the root logger. The
logger
created inemitters.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 tologging.basicConfig
inemitters.py
should be unnecessary.Environment (please complete the following information):
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.The text was updated successfully, but these errors were encountered: