【发布时间】:2014-04-23 09:26:35
【问题描述】:
编辑:我重组了问题并添加了标题,希望它更容易阅读
问题
我正在尝试向python decorator library 中的日志装饰器添加一些功能。
我想添加的选项之一是能够通过提供字典作为输入来设置日志记录级别。但是,无论我设置什么级别,它总是返回相同的结果。
失败的测试
运行下面的设置代码后,我正在通过运行以下命令对其进行测试:
@log_with(setConfig={'level':logging.INFO})
def c(msg):
print(msg)
c('OMG!!')
返回:
INFO:__main__:Running c
DEBUG:__main__:The following arguments have been received:# <-- This should not be here
('OMG!!',)
The following keyword arguments have been received:
{}
INFO:__main__:Returning c
OMG!!
如果这很重要,我正在以可移植、非注册的方式使用 WinPython 2.7.6。在 qtconsole 中测试失败
设置代码
import functools, logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
class log_with(object):
'''Logging decorator that allows you to log with a
specific logger or set one up on the go.
'''
def __init__(self,logger=None,funcentry='Running {}',funcexit='Returning {}',setConfig=None):
self.logger = logger
self.ENTRY_MESSAGE = funcentry
self.EXIT_MESSAGE = funcexit
self.setConfig = setConfig
def __call__(self, func):
'''Returns a wrapper that wraps func.
The wrapper will log the entry and exit points of the function
with specified level.
'''
# set logger if it was not set earlier
if not self.logger:
self.logger = logging.getLogger(func.__module__)
logging.basicConfig(**self.setConfig)
@functools.wraps(func)
def wrapper(*args, **kwds):
self.logger.info(self.ENTRY_MESSAGE.format(func.__name__)+'\n\n')
self.logger.debug("The following arguments have been received:\n{}\n\nThe following keyword arguments have been received:\n{}\n\n".format(args,kwds))
try:
f_result = func(*args, **kwds)
self.logger.info(self.EXIT_MESSAGE.format(func.__name__))
return f_result
except Exception:
self.logger.exception("An exception was raised:\n\n")
return wrapper
我的想法和尝试过的事情
重置所有处理程序
我试图通过删除所有可能存在的处理程序来修改装饰器中的 if not self.logger 循环,即
....
if not self.logger:
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
....
基于this SO answer,但这也不起作用,即输出保持不变。
我不懂装饰器和/或日志模块!
我删除了
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
然后再次运行代码。这次根本没有将日志消息打印到屏幕上。对我来说,这意味着 __call__ 方法中的 if not self.logger 循环有问题,即未创建记录器。
我不知道为什么.....
【问题讨论】:
-
似乎
__call__函数中的记录器没有正确创建...当我删除初始log=logging.getLogger(..)时,装饰器不起作用,即根本没有打印日志调用。 .. ???