While experimenting with python's logging module, I came across some unexpected behaviour. My guess is that this is due to my misunderstanding, rather than a bug, but I can't figure out what's wrong (and I've been careful to restart the python kernel before testing, to ensure that the root logger is only configured once.
import logging
logging.basicConfig(level=logging.CRITICAL)
logger1 = logging.getLogger(__name__)
logger1.debug("logger1 debug test")
As expected, the logging message is not output.
If I then change the level of the logger as follows:
logger1.setLevel('DEBUG')
logger1.debug("logger1 debug test")
I get the expected output:
DEBUG:__main__:logger1 debug test
However, when I now create a new logger object, it seems to have inherited the properties of logger1, rather than of root:
logger2 = logging.getLogger(__name__)
logger2.debug("logger2 debug test")
which produces:
DEBUG:__main__:logger2 debug test
Weirdly, logger2 still shows the root as the parent, yet shows a level of 10:
print(logger2.__dict__)
{'filters': [], 'name': '__main__', 'level': 10, 'parent': <RootLogger root (CRITICAL)>, 'propagate': True, 'handlers': [], 'disabled': False, '_cache': {10: True}, 'manager': <logging.Manager object at 0x00000265147F2B48>}
Can anyone please explain why logger2 isn't defaulting to a level of 0 (NOTSET)?