I saw a post from stackoverflow asking why his logger does not work as expect? The code is like:
import logging logger = logging.getLogger() logger.info("some message")
There is no message coming out. Okay, let’s check the level of this logger:
print(logger.level) # output is 30 (logging.WARNING)
Then we can just change its level to
DEBUG, it will certain work, right?
However, still no output! W*F, what happened?
According to official doc,
if you do not set an explicit handler for the logger, a special handler called
lastResort will be used.
See the code here. By default the logging level of
lastResort (it is stream handler) is 30.
We can change its level to output info message.
# setting both the logger and handler's level will work as expected. logger.setLevel(logging.DEBUG) logging.lastResort.setLevel(logging.DEBUG)
However, this is like a hack and never an encouraged solution.
If we want to do logging real quick, we can use the method
This will create the logger. The
logger.level will be the level we set here.
Without a level param, the default level for root logger is
A stream handler will be also created, with level
This can be verified by printing the logger’s handlers:
print(logger.handlers) # you will see: [<StreamHandler <stderr> (NOTSET)>]
- logger logging without handler: https://stackoverflow.com/q/64570918/6064933
License CC BY-NC-ND 4.0