Python 日志记录:使用 fileconfig 和编程配置之间的不同行为

发布于 2024-12-10 11:32:12 字数 1974 浏览 0 评论 0原文

我刚刚发现了 Python 日志记录的不同行为,具体取决于我是否使用文件配置日志记录和编程配置日志记录。

为了进行演示,我创建了两个最小的示例。

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

# 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 配置日志记录。据我所知,日志配置文件应该具有完全相同的行为。但是,此示例中仍然没有打印调试日志消息。

# 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,)

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-12-17 11:32:12

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

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

before

from foo import Foo

或调用

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

Since fileConfig disables existing loggers by default, call

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

before

from foo import Foo

or call

logging.config.fileConfig("logging.cfg",disable_existing_loggers=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文