0

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?

1 Answer 1

1

The reason you are seeing this is because in your format string you are explicitely asking for the pre-formatted raw string by using the msg key. If you change msg in that one line to message, which is the key for the formatted logstring, they both output "foo bar".

record_format = "(%(asctime)s %(module)s %(funcName)s) %(message)s"
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it had to be something simple. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.