I have a logger that uses a Formatter and needs to log a message that includes a variable. Normal enough. If my logger doesn't user a formatter, all is fine. If it does, suddenly it doesn't know how to do format string expansion any more. This is very weird.
So if I have this:
import logging
import sys
logger = logging.getLogger("test_logger")
normal_handler = logging.StreamHandler(sys.stdout)
formatted_handler = logging.FileHandler("testlog.txt")
logger.setLevel(logging.DEBUG)
normal_handler.setLevel(logging.DEBUG)
formatted_handler.setLevel(logging.DEBUG)
record_format = "(%(asctime)s %(module)s %(funcName)s) %(msg)s"
timestamp_format = "[%Y-%m-%d %H:%M:%S]"
formatter = logging.Formatter(fmt=record_format, datefmt=timestamp_format)
formatted_handler.setFormatter(formatter)
logger.addHandler(normal_handler)
logger.addHandler(formatted_handler)
test_var = "bar"
logger.info("foo %s", test_var)
...then on stdout I get the expected output: foo bar
...but in the log file that used the formatter I get: ([2021-03-30 11:20:29] <stdin> <module>) foo %s
I know the formatter comes with a style parameter, but a) I can't use it, because I have to support python 2 as well, and b) even if I did use it, python 2 has the same problem, so it wouldn't fix the issue anyway. I could pre-format my log messages before I send them to the logger, but I gather this is discouraged (and certainly pylint complains about it, and I don't really want to disable the pylint warning rather than fixing the issue).
What am I missing please?