Why do two loggers can behave differently?

Viewed 30

I have two loggings.Logger instances I'm testing in IPython. They do not have any handler, but rely on propagating the message towards the root logger, from which they are both descendents. One of them produces the expected output message, whereas the other produces nothing, and it should. This is driving me crazy. How can I figure out what's happening?

In commands:

logger_a = logging.getLogger("test")
logger_b = logging.getLogger("test2")

logger_a.warning("warning")  # prints out nothing  WHY???

ogger_b.warning("warning")  # prints the text below
20210616_180404:test2:WARNING:warning

# they equally have no handlers, but propapate towards root
logger_a.handlers == logger_b.handlers  # returns True, in both cases is []
logger_a.propagate  # returns True  
logger_b.propagate  # returns True

logger_a.parent
<RootLogger root (DEBUG)>  

logger_a.parent is logger_b.parent  # returns True

logger_a.parent.handlers
[<StreamHandler <stdout> (DEBUG)>]

Note that I'm running things in the terminal, so at this point I can not get a minimal example that reproduces this issue. Therefore my question is more in the line of: how can I figure out the current difference between these two, in principle equivalent, loggers?

1 Answers

Here's a self-contained script based on your post:

import logging

logger_a = logging.getLogger("test")
logger_b = logging.getLogger("test2")
logger_a.warning("warning1")
logger_b.warning("warning2")
assert logger_a.handlers == logger_b.handlers
assert logger_a.propagate
assert logger_b.propagate
assert logger_a.parent is logger_b.parent
print(logger_a.parent.handlers)

This prints out

warning1
warning2
[]

Note how both lines are printed, and there are no handlers on the root.

This means there has to be something else about your particular configuration we don't know about.

Is it possible there's a filter configured on either logger, or the root perhaps? Certainly something seems to have configured the root logger's handlers that you're not showing us.

Related