Python logging.info() not logging the message

2020-08-10 08:11发布

问题:

parser_logger = logging.getLogger("CSHEL_parserlogger");
#logging.basicConfig()
parser_logger.addHandler(RotatingFileHandler(
                            "logfile", mode='a', maxBytes=7340032, backupCount=4,
                            encoding=None, delay=False))

#d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
parser_logger.info('Protocol problem: %s', 'connection reset')

This would create a file named logfile, but won't write anything into it. If I change the last line to

parser_logger.warning('Protocol problem: %s', 'connection reset')

it would log the message into the "logfile" properly.

I am sure it's a petty thing that I am missing, but I am not able to figure out what it is.

回答1:

You need to set the threshold level of the logger:

parser_logger.setLevel(logging.INFO)

When a logger is created, the level is set to NOTSET, and the root logger is created with level WARNING. See the documentation.