For some reason my Python logger does not want to recognize microseconds format.
import logging, io
stream = io.StringIO()
logger = logging.getLogger("TestLogger")
logger.setLevel(logging.INFO)
logger.propagate = False
log_handler = logging.StreamHandler(stream)
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',"%Y-%m-%d %H:%M:%S.%f %Z")
log_handler.setFormatter(log_format)
logger.addHandler(log_handler)
logger.info("This is test info log")
print(stream.getvalue())
It returns:
2023-01-06 18:52:34.%f UTC - TestLogger - INFO - This is test info log
Why are microseconds missing?
Update
I am running
Python 3.10.4
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
2
Answers
The issue is that the
formatTime
method usestime.strptime
to format the currenttime.time()
, but sincestruct_time
has no information aboutmilliseconds
andmicroseconds
the formatter ignores the%f
.Also, note that the
LogRecord
calculates the milliseconds separately and stores them in another variable namedmsecs
To get what you’re looking for we need a custom version of the
Formatter
class that uses a different converter thantime.localtime
and is able to interpret the microseconds:Should output:
2023-01-06 17:47:54.828521 EST - TestLogger - INFO - This is test info log
I found that the answer given by Ashwini Chaudhary to not be applicable for me and found a slightly simpler solution in creating a function with the formatting as done with
datetime
and changing the value of thelogging.Formatter.formatTime
method to this instead i.e.:And then generating your logger as normal