2.5和2.6之间的Python日志记录不兼容性 [英] Python logging incompatibilty between 2.5 and 2.6

查看:129
本文介绍了2.5和2.6之间的Python日志记录不兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

logger.conf:

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:

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:

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:

logger.py:

from module_one import function_one
from module_two import function_two

function_one()
function_two()

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

Output of calling logger.py under Ubuntu 9.04:

$ python2.5 logger.py
$

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

推荐答案

这是一个已在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.

这篇关于2.5和2.6之间的Python日志记录不兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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