Python日志记录不输出任何东西 [英] Python logging not outputting anything

查看:1038
本文介绍了Python日志记录不输出任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的python脚本中,我试图使用日志记录模块记录事件.我有以下代码来配置我的记录器:

In a python script I am writing, I am trying to log events using the logging module. I have the following code to configure my logger:

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")时,即使

When I try to run logging.debug("Some string"), I get no output to the console, even though this page in the docs says that logging.debug should have the root logger output the message. Why is my program not outputting anything, and how can I fix it?

推荐答案

默认日志记录级别为警告. 由于您尚未更改级别,因此根记录器的级别仍处于警告状态. 这意味着它将忽略任何级别低于警告的日志记录,包括调试日志记录.

The default logging level is warning. Since you haven't changed the level, the root logger's level is still warning. That means that it will ignore any logging with a level that is lower than warning, including debug loggings.

教程中对此进行了解释:

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

信息"行不打印任何内容,因为级别高于信息.

The 'info' line doesn't print anything, because the level is higher than info.

要更改级别,只需在root记录器中进行设置:

To change the level, just set it in the root logger:

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

换句话说,仅用level = DEBUG定义处理程序是不够的,实际的日志记录级别也必须是DEBUG才能使其输出任何内容.

In other words, it's not enough to define a handler with level=DEBUG, the actual logging level must also be DEBUG in order to get it to output anything.

这篇关于Python日志记录不输出任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆