Python日志记录:使用fileconfig和编程配置之间的不同行为 [英] Python logging: different behavior between using fileconfig and programmatic config

查看:120
本文介绍了Python日志记录:使用fileconfig和编程配置之间的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了Python日志记录的另一种行为,具体取决于我是否将日志记录与fileconfig一起使用,并将日志记录与程序化config一起使用.

I just discovered a different behavior of Python logging, depending on if I use logging with fileconfig and logging with programmatic config.

为了演示,我创建了两个最小的示例.

To demonstrate, I created two minimal examples.

在第一个示例中,我以编程方式配置日志记录.此示例按预期方式工作-调试日志消息已打印到控制台.

In the first example I'm configuring logging programmatically. This example works as expected - the debug log message is printed to the console.

# foo.py
import logging

logger = logging.getLogger(__name__)

class Foo(object):
    def __init__(self):
        logger.debug('debug log from Foo')

##########################################################################
# loggingtest.py
import logging.config

from foo import Foo

if __name__ == '__main__':
    consoleLogger = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
    consoleLogger = logging.StreamHandler()
    consoleLogger.setLevel(logging.DEBUG)
    consoleLogger.setFormatter(formatter)

    rootLogger = logging.getLogger() 
    rootLogger.addHandler(consoleLogger)
    rootLogger.setLevel(logging.NOTSET)
    # prints debug log message to console
    foo = Foo()

在第二个示例中,我正在使用fileConfig配置日志记录.据我所知,log-config-file应该具有完全相同的行为.但是,在此示例中,调试日志消息仍未打印.

In my second example, I'm configuring logging with fileConfig. As far as I can see, the log-config-file should have the exact same behavior. But still, the debug log message is NOT printed in this example.

# foo.py (same as above)
import logging

logger = logging.getLogger(__name__)

class Foo(object):
    def __init__(self):
        logger.debug('debug log from Foo')
##########################################################################
# loggingtest.py
import logging.config

from foo import Foo

if __name__ == '__main__':
    logging.config.fileConfig('logging.cfg')    

    # does NOT print debug log message to console. WHY???
    foo = Foo()

##########################################################################
# logging.cfg
[loggers]
keys = root

[logger_root]
level = NOTSET
handlers = consoleHandler

[formatters]
keys = complex

[formatter_complex]
format = %(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s

[handlers]
keys = consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=complex
args=(sys.stdout,)

那么为什么第二个使用日志文件配置的示例不将我的调试日志消息打印到控制台?

So why is the second example using a logging fileconfig NOT printing my debug log message to the console?

推荐答案

由于默认情况下fileConfig禁用现有记录器,因此调用

Since fileConfig disables existing loggers by default, call

logging.config.fileConfig("logging.cfg")

之前

from foo import Foo

或致电

logging.config.fileConfig("logging.cfg",disable_existing_loggers=0)

这篇关于Python日志记录:使用fileconfig和编程配置之间的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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