Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
参数中包含函数的Python日志记录开销_Python_Logging - Fatal编程技术网

参数中包含函数的Python日志记录开销

参数中包含函数的Python日志记录开销,python,logging,Python,Logging,我正在使用python日志模块(python 3.4.3)。 假设我的日志级别设置为INFO,我想使用一个函数作为日志消息的参数 现在我希望当日志级别降低时(例如警告),参数函数不会执行 这是真的吗 示例(控制台): 该函数可能未执行,但仍然存在明显的延迟。有人能给我解释一下可能的原因吗?函数参数在调用前求值 您可以看到传递给logging.debug('Factorial of 100000=',math.Factorial(100000))的参数需要在调用logging.debug()之前进

我正在使用python日志模块(python 3.4.3)。 假设我的日志级别设置为INFO,我想使用一个函数作为日志消息的参数

现在我希望当日志级别降低时(例如警告),参数函数不会执行

这是真的吗

示例(控制台):

该函数可能未执行,但仍然存在明显的延迟。有人能给我解释一下可能的原因吗?

函数参数在调用前求值 您可以看到传递给
logging.debug('Factorial of 100000=',math.Factorial(100000))
的参数需要在调用
logging.debug()
之前进行计算,计算
math.Factorial(100000)
需要时间

如果函数最终在其调用中什么都不做,则参数仍然会得到计算

从概念上来说,你可以把它看作是在做

value = math.factorial(100000)           # This gets evaluated
logging.debug('Factorial is %d', value)  # Even if this is a "no op" call, 
                                         # the arg evaluation still takes place
变通办法
您可以看到我们的if块(使用
时)问题在于在调用函数之前对函数进行求值
如bakkal所述。这可能有点冗长。另一种解决方法是创建一个中间对象,传递给在其
\uuu str\uuu
函数中执行工作的记录器。这会延迟函数调用,直到日志模块确定消息将被发出

例如:

import logging
import math

class CallableMsg:
    def __init__(self, func, *args, **kwargs):
        self.func = func
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return str(self.func(*self.args, **self.kwargs))

logging.basicConfig(level=logging.INFO)

# single arg log
logging.info(CallableMsg(math.factorial, 10)) 
# using the CallableMsg in a format string
logging.info("10! = %s", CallableMsg(math.factorial, 10))
# factorial function never gets evaluated for the debug call
logging.debug(CallableMsg(math.factorial, 1000000))
如果出现错误,日志模块还可以保留实际进行日志调用的行

logging.info(CallableMsg(math.factorial, -1))
产出:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Python34\lib\logging\__init__.py", line 971, in emit
    msg = self.format(record)
  File "C:\Python34\lib\logging\__init__.py", line 821, in format
    return fmt.format(record)
  File "C:\Python34\lib\logging\__init__.py", line 558, in format
    record.message = record.getMessage()
  File "C:\Python34\lib\logging\__init__.py", line 319, in getMessage
    msg = str(self.msg)
  File "C:python\run.py", line 12, in __str__
    return str(self.func(*self.args, **self.kwargs))
ValueError: factorial() not defined for negative values
Call stack:
  File "<string>", line 1, in <module>
  File "C:\Python34\lib\idlelib\run.py", line 126, in main
    ret = method(*args, **kwargs)
  File "C:\Python34\lib\idlelib\run.py", line 353, in runcode
    exec(code, self.locals)
  File "C:\python\run.py", line 18, in <module>
    logging.info(CallableMsg(math.factorial, -1))
Message: <__main__.CallableMsg object at 0x02ECF970>
Arguments: ()
---日志记录错误---
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\logging\\uuuu init\uuuuu.py”,第971行,在emit中
msg=self.format(记录)
文件“C:\Python34\lib\logging\ \ uu init\ uu.py”,第821行,格式为
返回格式(记录)
文件“C:\Python34\lib\logging\\uuuu init\uuuuu.py”,第558行,格式为
record.message=record.getMessage()
文件“C:\Python34\lib\logging\\uuuu init\uuuuu.py”,第319行,在getMessage中
msg=str(self.msg)
文件“C:python\run.py”,第12行,在__
返回str(self.func(*self.args,**self.kwargs))
ValueError:未为负值定义阶乘()
调用堆栈:
文件“”,第1行,在
文件“C:\Python34\lib\idlelib\run.py”,第126行,在main中
ret=方法(*args,**kwargs)
运行代码中的文件“C:\Python34\lib\idlelib\run.py”,第353行
exec(代码,self.locals)
文件“C:\python\run.py”,第18行,在
logging.info(CallableMsg(math.factorial,-1))
信息:
参数:()
import logging
import math

class CallableMsg:
    def __init__(self, func, *args, **kwargs):
        self.func = func
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return str(self.func(*self.args, **self.kwargs))

logging.basicConfig(level=logging.INFO)

# single arg log
logging.info(CallableMsg(math.factorial, 10)) 
# using the CallableMsg in a format string
logging.info("10! = %s", CallableMsg(math.factorial, 10))
# factorial function never gets evaluated for the debug call
logging.debug(CallableMsg(math.factorial, 1000000))
logging.info(CallableMsg(math.factorial, -1))
--- Logging error ---
Traceback (most recent call last):
  File "C:\Python34\lib\logging\__init__.py", line 971, in emit
    msg = self.format(record)
  File "C:\Python34\lib\logging\__init__.py", line 821, in format
    return fmt.format(record)
  File "C:\Python34\lib\logging\__init__.py", line 558, in format
    record.message = record.getMessage()
  File "C:\Python34\lib\logging\__init__.py", line 319, in getMessage
    msg = str(self.msg)
  File "C:python\run.py", line 12, in __str__
    return str(self.func(*self.args, **self.kwargs))
ValueError: factorial() not defined for negative values
Call stack:
  File "<string>", line 1, in <module>
  File "C:\Python34\lib\idlelib\run.py", line 126, in main
    ret = method(*args, **kwargs)
  File "C:\Python34\lib\idlelib\run.py", line 353, in runcode
    exec(code, self.locals)
  File "C:\python\run.py", line 18, in <module>
    logging.info(CallableMsg(math.factorial, -1))
Message: <__main__.CallableMsg object at 0x02ECF970>
Arguments: ()