Why doesn't logging.setLevel () affect Python?
I am trying to understand how a module works logging
. The following code doesn't react the way I expected.
#!/usr/bin/env python3
import logging
l = logging.getLogger()
l.setLevel(logging.DEBUG)
print('enabled for DEBUG: {}'.format(l.isEnabledFor(logging.DEBUG)))
l.debug('debug')
l.info('info')
l.warning('warning')
l.error('error')
l.critical('critical')
Just print this to the console.
warning
error
critical
But why? It should not be info
, and debug
the same? Why not?
The question is not how to fix it. I know about handlers and the like. I am just trying to understand how this code works and why it is not responding the way I expect.
source to share
If no handler is installed, the handler is used lastResort
and the default for the level is lastResort
set to WARNING
.
This is implemented by this bit of code :
_defaultLastResort = _StderrHandler(WARNING)
lastResort = _defaultLastResort
def callHandlers(self, record):
...
found = 0
...
if (found == 0):
if lastResort:
if record.levelno >= lastResort.level:
lastResort.handle(record)
Remember also that both loggers and handlers are level . The entry can be filtered by the logger for too low level, and it can also be filtered by the handler for too low level. Setting the log level to DEBUG allows subsequent log calls to pass the log level filter, but they can be filtered by the default handler level filter lastResort.level
, i.e. WARNING
...
source to share