Python 日志记录 2.5 和 2.6 之间不兼容

发布于 2024-07-25 13:53:49 字数 1295 浏览 8 评论 0原文

您能帮我解决以下 Python 2.5 和 2.6 之间的不兼容问题吗?

logger.conf:

[loggers]
keys=root,aLogger,bLogger

[handlers]
keys=consoleHandler

[formatters]
keys=

[logger_root]
level=NOTSET
handlers=consoleHandler

[logger_aLogger]
level=DEBUG
handlers=consoleHandler
propagate=0
qualname=a

[logger_bLogger]
level=INFO
handlers=consoleHandler
propagate=0
qualname=b

[handler_consoleHandler]
class=StreamHandler
args=(sys.stderr,)

module_one.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

def function_one():
    b_log.info("function_one() called.")

module_two.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

def function_two():
    a_log.info("function_two() called.")

logger.py:

from module_one import function_one
from module_two import function_two

function_one()
function_two()

在 Ubuntu 9.04 下调用 logger.py 的输出:

$ python2.5 logger.py
$

$ python2.6 logger.py
function_one() called.
function_two() called.
$

Could you help me solve the following incompatibility issue between Python 2.5 and 2.6?

logger.conf:

[loggers]
keys=root,aLogger,bLogger

[handlers]
keys=consoleHandler

[formatters]
keys=

[logger_root]
level=NOTSET
handlers=consoleHandler

[logger_aLogger]
level=DEBUG
handlers=consoleHandler
propagate=0
qualname=a

[logger_bLogger]
level=INFO
handlers=consoleHandler
propagate=0
qualname=b

[handler_consoleHandler]
class=StreamHandler
args=(sys.stderr,)

module_one.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

def function_one():
    b_log.info("function_one() called.")

module_two.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

def function_two():
    a_log.info("function_two() called.")

logger.py:

from module_one import function_one
from module_two import function_two

function_one()
function_two()

Output of calling logger.py under Ubuntu 9.04:

$ python2.5 logger.py
$

$ python2.6 logger.py
function_one() called.
function_two() called.
$

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

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

发布评论

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

评论(4

奈何桥上唱咆哮 2024-08-01 13:53:49

这是一个在 2.5 和 2.6 之间修复的错误。 fileConfig() 函数用于一次性配置,因此不应多次调用 - 无论您选择如何安排。 fileConfig 的预期行为是禁用配置中未明确提及的任何记录器,并启用提到的记录器及其子记录器; 这个错误导致孩子们在不应该的情况下残疾。 示例记录器配置提到了记录器“a”和“b”; 调用 getLogger('a.submod') 后,将创建一个子记录器。 第二个 fileConfig 调用在 Python 2.5 中错误地禁用了此功能 - 在 Python 2.6 中,未禁用记录器,因为它是配置中明确提到的记录器的子项。

This is a bug which was fixed between 2.5 and 2.6. The fileConfig() function is intended for one-off configuration and so should not be called more than once - however you choose to arrange this. The intended behaviour of fileConfig is to disable any loggers which are not explicitly mentioned in the configuration, and leave enabled the mentioned loggers and their children; the bug was causing the children to be disabled when they shouldn't have been. The example logger configuration mentions loggers 'a' and 'b'; after calling getLogger('a.submod') a child logger is created. The second fileConfig call wrongly disables this in Python 2.5 - in Python 2.6 the logger is not disabled as it is a child of a logger explicitly mentioned in the configuration.

流心雨 2024-08-01 13:53:49

我自己不明白这种行为的原因,但正如你在 2.6 中所说的那样,它的工作方式有所不同。 我们可以假设这是一个影响 2.5 的错误

作为解决方法,我建议如下:

extra_module.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

module_one.py:

from extra_module import a_log

def function_one():
    a_log.info("function_one() called.")

module_two.py:

from extra_module import b_log

def function_two():
    b_log.info("function_two() called.")

通过使用此方案,我能够在 python2.5.4 上运行 logger.py,其行为与共 2.6

I don't understand the reasons of this behavior myself but as you well stated in 2.6 it works differently. We can assume this is a bug affecting 2.5

As a workaround I suggest the following:

extra_module.py:

import logging
import logging.config

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a.submod')
b_log = logging.getLogger('b.submod')

module_one.py:

from extra_module import a_log

def function_one():
    a_log.info("function_one() called.")

module_two.py:

from extra_module import b_log

def function_two():
    b_log.info("function_two() called.")

by using this scheme I was able to run logger.py on python2.5.4 with the same behavior as of 2.6

夏了南城 2024-08-01 13:53:49

有趣...我在控制台中玩了一会儿,看起来对 logging.config.fileConfig 的第二次调用把事情搞砸了。 但不确定为什么会这样......这是显示问题的记录:

lorien$ python2.5
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> import logging.config
>>> logging.config.fileConfig('logger.conf')
>>> alog = logging.getLogger('a.submod')
>>> alog.info('foo')
foo
>>> import logging
>>> import logging.config
>>> alog.info('foo')
foo
>>> logging.config.fileConfig('logger.conf')
>>> alog.info('foo')
>>> alog = logging.getLogger('a.submod')
>>> alog.info('foo')
>>> 
>>> blog = logging.getLogger('b.submod')
>>> blog.info('foo')
foo
>>>

当我第二次调用 logging.config.fileConfig 时,我的记录器实例就会停止记录。 获取新的日志记录实例没有帮助,因为它是同一个对象。 如果我等到配置两次后才获取记录器实例,那么事情就会起作用 - 这就是 blog 实例起作用的原因。

我的建议是延迟获取记录器实例,直到您进入函数。 如果将对 logging.getLogger() 的调用移至 function_onefunction_two 中,则一切正常。

Interesting... I played a little in the console and it looks like the second call to logging.config.fileConfig is mucking things up. Not sure why this is though... Here's a transcript that shows the problem:

lorien$ python2.5
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> import logging.config
>>> logging.config.fileConfig('logger.conf')
>>> alog = logging.getLogger('a.submod')
>>> alog.info('foo')
foo
>>> import logging
>>> import logging.config
>>> alog.info('foo')
foo
>>> logging.config.fileConfig('logger.conf')
>>> alog.info('foo')
>>> alog = logging.getLogger('a.submod')
>>> alog.info('foo')
>>> 
>>> blog = logging.getLogger('b.submod')
>>> blog.info('foo')
foo
>>>

As soon as I call logging.config.fileConfig the second time, my logger instance stops logging. Grabbing a new logging instance doesn't help since it's the same object. If I wait until after configuring both times to fetch the logger instances, then things work - this is why the blog instance works.

My suggestion is to delay grabbing the logger instances until you are in the functions. If you move the calls to logging.getLogger() into function_one and function_two, then everything works well.

微凉徒眸意 2024-08-01 13:53:49

我可以通过在两个文件中更改记录器的名称来解决此问题:

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a')
b_log = logging.getLogger('b')

我不确定确切的错误,但 v2.5 记录器模块似乎在匹配传递给 getLogger( ) 以及配置文件中的名称。

I was able to fix this by changing the names of the loggers like so, in both files:

logging.config.fileConfig('logger.conf')
a_log = logging.getLogger('a')
b_log = logging.getLogger('b')

I'm not sure of the exact error, but the v2.5 logger module seems to have trouble matching names passed to getLogger() with names in the config file.

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