In the logging howto documentation there is this example:
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
Why I should set the level to logging.DEBUG
twice, for Logger
, and for the StreamHandler
?
I understand ch.setLevel(logging.DEBUG)
will set the debug level for the stream handler. But what the effect is of setting the level to logger? Where this level is reflected?
I get the same console output if I change the level to, for example, INFO
either to the Logger
or to the StreamHandler
.
That is:
...........
logger.setLevel(logging.INFO)
............
ch.setLevel(logging.DEBUG)
gives the same output in console than
...........
logger.setLevel(logging.DEBUG)
............
ch.setLevel(logging.INFO)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…