Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging setLevel is being ignored

Tags:

python

logging

The below code is copied from the documentation. I am supposed to be able to see all the info logs. But I don't. I am only able to see the warn and above even though I've 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 did the info and debug messages go??

like image 730
meh Avatar asked Mar 30 '17 05:03

meh


2 Answers

Replace the line

logger.setLevel(logging.DEBUG) 

with

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

and it should work as expected. If you don't configure logging with any handlers (as in your post - you only configure a level for your logger, but no handlers anywhere), you'll get an internal handler "of last resort" which is set to output just the message (with no other formatting) at the WARNING level.

like image 129
Vinay Sajip Avatar answered Sep 30 '22 18:09

Vinay Sajip


Try running logging.basicConfig() in there. Of note, I see you mention INFO, but use DEBUG. As written, it should show all five messages. Swap out 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 have logging set up elsewhere in your code already? Can't reproduce the exact behavior you note with the specific code provided.

like image 39
tabbek Avatar answered Sep 30 '22 19:09

tabbek