python logger.debug_Python Logger两次将内容记录到控制台

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")

Here's my config file:

[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

Any ideas?

解决方案

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

The logging module's docs say:

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.

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

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

[logger_root]

handlers=

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值