日志记录不输出调试和信息日志



我写了以下代码:

import logging
logging.basicConfig(filename='rb-log.log', 
format='%(process)d-%(asctime)s-%(levelname)s-%(message)s',
level=logging.DEBUG)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

日志文件包含:

646-2021-10-30 22:08:39,404-WARNING-This is a warning message
646-2021-10-30 22:08:39,406-ERROR-This is an error message
646-2021-10-30 22:08:39,407-CRITICAL-This is a critical message

Only WARNING, ERROR &正在将关键日志写入日志文件。调试,不写INFO日志。我哪里做错了?

您没有显示如何设置logger,因此它看起来在配置之前。配置(logging.basicConfig(...))将不适用于已经设置的记录器;您的日志记录器正在使用默认级别WARN。

您可以使用logging上的便利方法,如下所示https://docs.python.org/3/howto/logging.html logging-to-a-file

logging.basicConfig(filename='rb-log.log', 
format='%(process)d-%(asctime)s-%(levelname)s-%(message)s',
level=logging.DEBUG)
logging.debug('This is a debug message')
logging.info('This is an info message')

或在创建记录器之前配置日志记录:

logging.basicConfig(filename='rb-log.log', 
format='%(process)d-%(asctime)s-%(levelname)s-%(message)s',
level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug('This is a debug message')
logger.info('This is an info message')

相关内容

  • 没有找到相关文章

最新更新