Python:如何进行延迟调试日志记录
作者:互联网
我有一些像这样的python:
def foo():
logger = logging.getLogger()
# do something here
logger.debug('blah blah {}'.format(expensive_func()))
foo()
其中expensive_func()是一个返回字符串的函数,执行起来很昂贵.
在开发时,日志级别设置为DEBUG,并且执行expensive_func(),消息被记录,一切都很好.
问题是,当我将日志级别设置为严格大于DEBUG(例如WARNING)时,在生产环境中,显然不会记录expensive_func()的返回值,但仍会执行昂贵的函数本身.
我的问题是:当日志级别为WARNING时,如何防止python出现昂贵的函数?
我不想删除该调试行或添加类似if level>的内容. DEBUG:返回昂贵的功能.
谢谢.
编辑
我刚刚访问了Lazy logger message string evaluation,但对此并不满意,主要是因为:
>这是一些丑陋的东西;
>即使我用一些Lazy类包装昂贵的函数,当我有两个昂贵的函数时,我会怎么做? (如下所示).
class Lazy:
def __init__(self, func, *a, **ka):
self.func= func
self.a = a
self.ka= ka
def __str__(self):
return str(self.func(*self.a, **self.ka))
# Though this is ugly, it works
logger.debug('Message: %s', Lazy(expensive_func))
# What if I wanted to do this?
# logger.debug('Message: {}'.format(expf_1(expf_2(some_arg))))
# Maybe I can modify class Lazy to make something like this to work
# but it really doesn't feel right
# logger.debug('Message: {}', Lazy(expf_1, Lazy(expf_2, some_arg)))
解决方法:
查看文档的this part.
更新:日志记录已经支持延迟评估,但与您的comnment中描述的方式略有不同.例如,请参阅以下脚本:
import logging
def expensive_func(*args):
print('Expensive func called: %s' % (args,))
return sum(args)
class DeferredMessage(object):
def __init__(self, func, *args):
self.func = func
self.args = args
def __str__(self):
return 'Message {0}'.format(self.func(*self.args))
if __name__ == '__main__':
logging.basicConfig()
logging.info(DeferredMessage(expensive_func, 1, 2))
logging.warning(DeferredMessage(expensive_func, 3, 4))
logging.error(DeferredMessage(expensive_func, 5, 6))
运行上面的脚本时,应该打印
Expensive func called: (3, 4)
WARNING:root:Message 7
Expensive func called: (5, 6)
ERROR:root:Message 11
这表明只在必要时调用潜在的昂贵功能.当然,上面的示例可以推广为允许将格式字符串传递给DeferredMessage,并使用kwargs等等.
标签:python,logging,lazy-evaluation 来源: https://codeday.me/bug/20191006/1862038.html