Logging setLevel is ignored

The following code is copied from the documentation. I should see all the news magazines. But I do not. I see only a warning and above, although I set setLevel to INFO.

Why is this happening? foo.py :

 import logging logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message') 

Output:

 workingDirectory$ python foo.py warn message error message critical message 

Where were the information and debug messages sent?

+19
source share
4 answers

Replace line

 logger.setLevel(logging.DEBUG) 

with

 logging.basicConfig(level=logging.DEBUG, format='%(message)s') 

and this should work as expected. If you do not configure logging using any handlers (as in your post - you only configure the level for your registrar, but do not use handlers anywhere), you will get an internal "last resort" handler that is configured to output only a message (without other formatting) at the WARNING level.

+12
source

Try running logging.basicConfig() there. It should be noted that you mention INFO but use DEBUG. As written, all five messages should be displayed. Replace DEBUG with INFO, and you should see four messages.

 import logging logging.basicConfig() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message') 

edit: do you already have a registration elsewhere in your code? It is not possible to reproduce the exact behavior that you noticed with the specified code.

+16
source

Technically, this is also the β€œanswer” because it can β€œsolve” the problem. BUT I definitely don't like it. This is not intuitive, and I lost 2+ hours because of this.

Before:

 import logging logger = logging.getLogger('foo') logger.setLevel(logging.INFO) logger.info('You can not see me') # Or you can just use the following one-liner in command line. # $ python -c "import logging; logger = logging.getLogger('foo'); logger.setLevel(logging.INFO); logger.info('You can not see me')" 

After:

 import logging logging.debug('invisible magic') # <-- magic logger = logging.getLogger('foo') logger.setLevel(logging.INFO) logger.info('But now you can see me') # Or you can just use the following one-liner in command line. $ python -c "import logging; logging.debug('invisible magic'); logger = logging.getLogger('foo'); logger.setLevel(logging.INFO); logger.info('But now you see me')" 

PS: Comparing it with the current selected answer and @ Vinay-Sip's explanation, I can understand why. But still, I would like this to not work.

0
source

The accepted answer does not work for me on Win10, Python 3.7.2.

My decision:

 logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) 

This order is sensitive.

0
source

Source: https://habr.com/ru/post/1266104/


All Articles