Python Logger两次将内容记录到控制台 [英] Python Logger logging things twice to console

查看:261
本文介绍了Python Logger两次将内容记录到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python编写一个记录器.我正在2.6中工作,所以我不能使用新的字典样式方法,而是要使用老式的配置文件.问题是,东西两次输出到控制台,我不明白为什么.这是我的测试脚本:

I'm trying to put together a logger using Python. I'm working in 2.6 so I can't use the new dictionary style method and instead am going with the good old fashioned config file. The problem is, stuff outputs twice to the console and I can't understand why. Here's my test script:

import logging
import logging.config

if __name__ == "__main__":
    logging.config.fileConfig("newSlogger.conf")
    slogger = logging.getLogger("sloggerMain")

    slogger.debug("dbg msg")
    slogger.info("herp derp dominae")

这是我的配置文件:

[loggers]
keys=root,sloggerMain,sloggerSecondary

[handlers]
keys=consoleHandler,infoFileHandler,debugFileHandler

[formatters]
keys=consoleFormatter,infoFileFormatter,debugFileFormatter

[logger_root]
handlers=consoleHandler
level=NOTSET

[logger_sloggerMain]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerMain

[logger_sloggerSecondary]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerSecondary

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
format=consoleFormatter
args=(sys.stdout,)

[handler_infoFileHandler]
class=FileHandler
level=INFO
formatter=infoFileFormatter
args=("testlog.log", "w")

[handler_debugFileHandler]
class=FileHandler
level=DEBUG
formatter=debugFileFormatter
args=("testlogdbg.log", "w")

[formatter_consoleFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_infoFileFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_debugFileFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_syslogFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

有什么想法吗?

推荐答案

更改您的非root记录器,将propagate设置为0,以防止消息传播到root记录器:

Change your non-root loggers to set propagate to 0, to prevent messages from being propagated up to the root logger:

[logger_sloggerMain]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerMain
propagate=0

logging模块的文档说:

子记录器将消息传播到 与他们相关联的处理程序 祖先记录器.因此,它 不需要定义和配置 所有记录器的处理程序 应用程序使用.足以 为顶级配置处理程序 记录器并创建子记录器为 需要.

Child loggers propagate messages up to the handlers associated with their ancestor loggers. Because of this, it is unnecessary to define and configure handlers for all the loggers an application uses. It is sufficient to configure handlers for a top-level logger and create child loggers as needed.

sloggerMain记录器是root记录器的子级.默认情况下,发送到该记录器的消息也会向上传播.

The sloggerMain logger is a child of the root logger. By default, messages emitted to that logger are also propagated upward.

您也可以简单地禁用root日志记录来解决该问题:

You can also simply disable root logging to fix the problem:

[logger_root]
handlers=

这篇关于Python Logger两次将内容记录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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