Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does logging.setLevel() has no effect here with Python?

I try to understand how the logging module really works. The following code doesn't react like 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')

It just print out this to the console.

warning
error
critical

But why? Shouldn't there be info and debug, too? Why not?

The question is not how to fix this. I know about handlers and things like that. I just try to understand how this code work and why it doesn't react like I expect.

like image 811
buhtz Avatar asked Aug 09 '15 00:08

buhtz


People also ask

What is logger setLevel Python?

The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on." Check under Handlers: http://docs.python.org/2.7/howto/logging.html#logging-advanced-tutorial.

How do I enable logging in Python?

You can configure logging as shown above using the module and class functions or by creating a config file or a dictionary and loading it using fileConfig() or dictConfig() respectively. These are useful in case you want to change your logging configuration in a running application.

How does Python logging work?

Logging Levels When you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level on up. If you set the log level to INFO, it will include INFO, WARNING, ERROR, and CRITICAL messages. NOTSET and DEBUG messages will not be included here.

How do you create a logging level in Python?

The logging level is set with setLevel . It sets the threshold for this logger to lvl . Logging messages which are less severe than lvl will be ignored. In the example, we change the logging level to DEBUG .


1 Answers

When no handler is set, the lastResort handler is used, and by default the lastResort level is 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 have levels. A record can be filtered by the logger for having too low a level, and it can also be filtered by a handler for having too low a level. Setting the logger level to DEBUG allows the subsequent logging calls to pass the logger's level filter, but they can still be filtered by the handler's level filter, which is set to lastResort.level, i.e. WARNING, by default.

like image 73
unutbu Avatar answered Oct 14 '22 03:10

unutbu