在我正在编写的 python 脚本中,我正在尝试使用日志记录模块记录事件。我有以下代码来配置我的记录器:
ERROR_FORMAT = "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d: %(message)s"
DEBUG_FORMAT = "%(lineno)d in %(filename)s at %(asctime)s: %(message)s"
LOG_CONFIG = {'version':1,
'formatters':{'error':{'format':ERROR_FORMAT},
'debug':{'format':DEBUG_FORMAT}},
'handlers':{'console':{'class':'logging.StreamHandler',
'formatter':'debug',
'level':logging.DEBUG},
'file':{'class':'logging.FileHandler',
'filename':'/usr/local/logs/DatabaseUpdate.log',
'formatter':'error',
'level':logging.ERROR}},
'root':{'handlers':('console', 'file')}}
logging.config.dictConfig(LOG_CONFIG)
当我尝试运行 logging.debug("Some string")
时,控制台没有输出,即使 文档中的此页面 说 logging.debug
应该让根记录器输出消息。为什么我的程序没有输出任何东西,我该如何解决?
原文由 murgatroid99 发布,翻译遵循 CC BY-SA 4.0 许可协议
默认的日志记录级别是警告。由于您没有更改级别,根记录器的级别仍然是警告。这意味着它将忽略任何级别低于警告的日志记录,包括调试日志记录。
这在 教程 中有解释:
‘info’ 行不打印任何内容,因为级别高于 info。
要更改级别,只需在根记录器中设置它:
换句话说,定义一个 level=DEBUG 的处理程序是不够的,实际的日志记录级别也必须是 DEBUG 才能让它输出任何东西。