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??
The accepted answer does not work for me on Win10, Python 3.7.2.
My solution:
It's order sensitive.
As pointed by some users, using:
like written in the accepted answare is not a goot option because it sets the loglevel globally, so it will log debug message from every logger.
In my case the best solution to set log level just for my logger was:
Not really intuitive solution, but is necessary if you want to set loglevel only for 'MyLogger' and not globally.
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.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.
This is technically also an "answer", because it can "solve" the problem. BUT I definitely DO NOT like it. It is not intuitive, and I lost 2+ hours over it.
Before:
After:
PS: Comparing it to the current chosen answer, and @Vinay-Sajip's explanation, I can kind of understand why. But still, I wish it was not working that way.
Replace the line
with
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.