python - PyLint 消息 : logging-format-interpolation

标签 python python-3.x pylint

对于以下代码:

logger.debug('message: {}'.format('test'))

pylint 产生以下警告:

logging-format-interpolation (W1202):

Use % formatting in logging functions and pass the % parameters as arguments Used when a logging statement has a call form of “logging.(format_string.format(format_args...))”. Such calls should use % formatting instead, but leave interpolation to the logging function by passing the parameters as arguments.

我知道我可以关闭此警告,但我想了解它。我假设使用 format() 是在 Python 3 中打印语句的首选方式。为什么对于 logger 语句不适用?

最佳答案

logger 语句不是这样,因为它依赖于以前的 "%"格式(如字符串)使用给予 logger 调用的额外参数来提供此字符串的惰性插值。例如,不要这样做:

logger.error('oops caused by %s' % exc)

你应该这样做

logger.error('oops caused by %s', exc)

所以只有在实际发出消息时才会对字符串进行插值。

使用 .format() 时,您无法享受此功能。


根据 Optimization logging 文档的部分:

Formatting of message arguments is deferred until it cannot be avoided. However, computing the arguments passed to the logging method can also be expensive, and you may want to avoid doing it if the logger will just throw away your event.

关于python - PyLint 消息 : logging-format-interpolation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619790/

相关文章:

plone - pylinting zopeish 文件 : F0401: 11, 0:无法导入 'zope.interface'

python - (Pyspark - 在一段时间内按用户分组

python - 交互 python 和 abaqus

python - 在从 pydantic.BaseModel 创建的 JSON 中,如果未设置,则排除可选

python - 视觉代码 pylint : unable to import webapp2 and google. appengine.api

python - Pylint 与 pytorch : Is there a way to tell pylint to look for module in different place?

python - pytorch "log_softmax_lastdim_kernel_impl"没有为 'torch.LongTensor' 实现

c++ - 如何在 C++ 或 Python 中验证图像文件的完整性?

python-3.x - 如何在非默认返回的FastAPI中指定response_model?

python-2.7 - pyclipper 中无法缩放 - TypeError : 'Zero' object is not iterable