Python logger.debug将参数转换为字符串而不进行日志记录 [英] Python logger.debug converting arguments to string without logging
问题描述
我正在优化执行某种计算的Python程序.它相当广泛地使用了NumPy.该代码中充满了 logger.debug
调用(logger是标准的Python日志对象).
I'm optimizing a Python program that performs some sort of calculation. It uses NumPy quite extensively. The code is sprinkled with logger.debug
calls (logger is the standard Python log object).
当我运行cProfile时,我看到将数组转换为字符串的Numpy函数花费了50%的执行时间.这令人惊讶,因为没有处理程序将消息输出为DEBUG级别,只有INFO或更高级别.
When I run cProfile I see that Numpy's function that converts an array to string takes 50% of the execution time. This is surprising, since there is no handler that outputs messages as the DEBUG level, only INFO and above.
即使没有人使用该字符串,记录器为什么还要将其参数转换为字符串?有没有办法防止它发生(除了不执行记录程序调用)?
Why is the logger converting its arguments to string even though nobody is going to use this string? Is there a way to prevent it (other than not performing the logger calls)?
推荐答案
没有看到您的代码,很难分辨正在发生什么,但是要查看两者的 logging .__ init __.py
的源代码.Python2.7和Python3.4(我所躺在的地方)显示,例如,对 logger.debug(...)
的调用看起来像:
Without seeing your code, it's hard to tell what's happening, but looking at the source code for logging.__init__.py
for both Python2.7 and Python3.4 (what I had lying around) shows that a call to logger.debug(...)
for example, looks like:
if self.isEnabledFor(DEBUG):
self._log(DEBUG, msg, args, **kwargs)
这暗示着,如果未启用 DEBUG
,则 logger
不会对任何参数进行任何处理.
Which implies that if DEBUG
isn't enabled, the logger
isn't going to do any processing on any of the arguments.
一个简短的注释,尽管我之前很忙,但是,在调用 logging.debug(...)
之前,您可能不小心构建了字符串.注意区别:
One quick note, that I've burned myself on before though, is that you may be accidentally building the string before ever calling logging.debug(...)
. Note the difference:
logger.debug("My big array: %s" % myBigArray) ## DON'T DO THIS
logger.debug("My big array: %s", myBigArray) ## much better
不同之处在于第一行甚至在进入 logger.debug
之前就进行了字符串格式化-您传递的是一个(可能是大的)字符串参数.另一方面,仅在启用 DEBUG
的情况下,第二行才会进行字符串格式化.
The difference is the first line does the string-formatting before it ever even gets into logger.debug
- you're passing in a single argument, which is a (potentially large) string. The second line, on the other hand, will do the string-formatting only if DEBUG
is enabled.
这篇关于Python logger.debug将参数转换为字符串而不进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!