【发布时间】:2021-02-06 00:36:11
【问题描述】:
我花了太多时间在这上面,我自己也搞不清楚。
我正在编写一些使用某些模块的代码,所以我想为我的消息创建一个特定的记录器,而不是来自其他模块的记录器(它们太多了,所以我不想为每个模块设置不同的级别其中)。
我已将我的用例简化为:
import logging
logger = logging.getLogger('test')
logger.setLevel(level=logging.INFO)
print(logging.Logger.manager.loggerDict)
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')
当我执行前面的代码时,我希望我的信息消息被记录下来。
相反,我得到了:
python3 test.py
{'test': <Logger test (INFO)>}
This is a warning message
This is an error message
This is a critical message
与DEBUG 相同,它总是打印警告/错误/关键消息,而不是调试/信息。
如果我提高日志记录级别(比如说ERROR),它会按预期工作。
python --version
Python 3.9.1
【问题讨论】: