2011-08-10 87 views
33

在我正在写的python脚本中,我正在尝试使用日志记录模块来记录事件。我有以下代码来配置我的记录: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"),我没有得到任何输出到控制台,即使this page in the docslogging.debug应该有根记录器输出的消息。为什么我的程序不输出任何内容,我该如何解决?

回答

54

默认日志记录级别是警告。 由于您没有更改关卡,因此根记录器的级别仍然是警告。 这意味着它将忽略任何级别低于警告的日志记录,包括调试日志记录。

这在tutorial解释:

import logging 
logging.warning('Watch out!') # will print a message to the console 
logging.info('I told you so') # will not print anything 

“信息”行不显示任何信息,因为水平比信息更高。

要更改级别,设置它的根记录:

'root':{'handlers':('console', 'file'), 'level':'DEBUG'} 

换句话说,这是不够的定义与水平= DEBUG处理程序,实际的日志记录级别也必须为了DEBUG让它输出任何东西。

+0

该文档说它的默认级别是NOTSET,它是0的级别,它应该输出所有内容...为什么不是这样? – Ben

+0

@Ben它说的那个?我所能看到的只是“默认级别是WARNING,这意味着只有该级别及以上的事件才会被跟踪,除非日志包被配置为其他方式。” –

+0

https://docs.python.org/3.6/library/logging.html#logging.Logger.setLevel – Ben