I am using Python 3.8.8 with the included logging library. To have a common string format in Python, I wanted to use also for logging the new format string variant with {}
. I also want to use lazy evaluation of the logging string (so no "{}".format(var)
or f"{var}"
).
According to this Stack Overflow post about pylint (so pylint logging-format-style=new
), the following variant should work. However, there is an exception at logging.info
.
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
var1 = "foo"
var2 = "blub"
logger.info("var1 {}, var2 {}", var1, var2) # Exception thrown!
The exception in the last line is as follows:
--- Logging error ---
Traceback (most recent call last):
File "/usr/local/lib/python3.8/logging/__init__.py", line 1081, in emit
msg = self.format(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 925, in format
return fmt.format(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 664, in format
record.message = record.getMessage()
File "/usr/local/lib/python3.8/logging/__init__.py", line 369, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
I infered from the exception message that logging still expects the old style formatting with %s
.
How can I use the new format string variant with {}
at logging in Python 3.8.8 without using any additional wrapper function (as suggested by some other posts on Stack Overflow)? Although possible to define in pylint, is this maybe even not possible without any wrapper function?