python logging 不输出控制台,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)

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.

This is explained in the tutorial:

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.

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

'root':{'handlers':('console', 'file'), 'level':'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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值