For the following code:
logger.debug('message: {}'.format('test'))
pylint
produces the following warning:
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.
I know I can turn off this warning, but I like to understand it. I assumed using format()
is the preferred way to print out statements in python3
. Why is this not true for logger statements?
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:
you should do
so the string will only be interpolated if the message is actually emitted.
You can't benefit of this functionality when using
.format()
.Per the Optimization section of the
logging
docs: