Python PyLint 消息:日志格式插值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34619790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyLint message: logging-format-interpolation
提问by pfnuesel
For the following code:
对于以下代码:
logger.debug('message: {}'.format('test'))
pylint
produces the following warning:
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.
日志格式插值(W1202):
在日志函数中使用 % 格式并将 % 参数作为参数传递当日志语句具有“logging.(format_string.format(format_args...))”的调用形式时使用。此类调用应改用 % 格式,但通过将参数作为参数传递来将插值留给日志记录函数。
I know I can turn off this warning, but I'd like to understand it. I assumed using format()
is the preferred way to print out statements in Python 3. Why is this not true for logger statements?
我知道我可以关闭此警告,但我想了解它。我认为 usingformat()
是在 Python 3 中打印语句的首选方式。为什么这对于 logger 语句来说不是这样?
采纳答案by sthenault
It is not true for logger statement because it relies on former "%" format like string to provide lazy interpolation of this string using extra arguments given to the logger call. For instance instead of doing:
对于 logger 语句来说不是这样,因为它依赖于以前的“%”格式,如字符串,使用给 logger 调用的额外参数提供此字符串的延迟插值。例如,而不是做:
logger.error('oops caused by %s' % exc)
you should do
你应该做
logger.error('oops caused by %s', exc)
so the string will only be interpolated if the message is actually emitted.
所以只有在实际发出消息时才会插入字符串。
You can't benefit of this functionality when using .format()
.
使用.format()
.
Per the Optimizationsection of the logging
docs:
根据文档的优化部分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.
消息参数的格式化被推迟,直到无法避免。但是,计算传递给日志记录方法的参数也可能很昂贵,如果记录器只会丢弃您的事件,您可能希望避免这样做。
回答by mustafagok
Maybe this time differencescan help you.
也许这个时差可以帮助你。
Following description is not the answer for your question, but it can help people.
以下描述不是您问题的答案,但可以帮助人们。
For pylint 2.4: There are 3 options for logging style in the .pylintrc
file: old
, new
, fstr
对于 pylint 2.4:文件中有 3 个用于记录样式的.pylintrc
选项:old
, new
,fstr
fstr
option added in 2.4and removed in 2.5
Description from .pylintrc
file (v2.4):
来自.pylintrc
文件 (v2.4) 的描述:
[LOGGING]
# Format style used to check logging format string. `old` means using %
# formatting, `new` is for `{}` formatting,and `fstr` is for f-strings.
logging-format-style=old
for old(logging-format-style=old
):
对于旧的( logging-format-style=old
):
foo = "bar"
self.logger.info("foo: %s", foo)
for new(logging-format-style=new
):
对于新的( logging-format-style=new
):
foo = "bar"
self.logger.info("foo: {}", foo)
# OR
self.logger.info("foo: {foo}", foo=foo)
Note: you can notuse .format()
even if you select new
option.
注意:即使您选择了选项,您也无法使用。.format()
new
pylint still gives the same warningfor this code:
pylint 仍然对此代码给出相同的警告:
self.logger.info("foo: {}".format(foo)) # W1202
# OR
self.logger.info("foo: {foo}".format(foo=foo)) # W1202
for fstr(logging-format-style=fstr
):
对于fstr( logging-format-style=fstr
):
foo = "bar"
self.logger.info(f"foo: {foo}")
Personally, I prefer fstr option because of PEP-0498.
就个人而言,我更喜欢 fstr 选项,因为PEP-0498。
回答by Tristan Crockett
In my experience a more compelling reason than optimization (for most use cases) for the lazy interpolation is that it plays nicely with log aggregators like Sentry.
根据我的经验,比优化(对于大多数用例)懒惰插值更引人注目的原因是它与 Sentry 等日志聚合器配合得很好。
Consider a 'user logged in' log message. If you interpolate the user into the format string, you have as many distinct log messages as there are users. If you use lazy interpolation like this, the log aggregator can more reasonably interpret this as the same log message with a bunch of different instances.
考虑“用户登录”日志消息。如果您将用户插入到格式字符串中,您将拥有与用户一样多的不同日志消息。如果您像这样使用延迟插值,日志聚合器可以更合理地将其解释为具有一堆不同实例的相同日志消息。