Following Python's documentation, I'm trying to override logging.Formatter.converter
in order to control the time logged.
As you can see below - the milliseconds were not overriden (they are the current time milliseconds).
How come? How can I control the milliseconds as well?
>>> import logging, datetime
>>> formatter = logging.Formatter('%(asctime)s:%(message)s')
>>> handler = logging.StreamHandler()
>>> handler.setFormatter(formatter)
>>> def sim_time(t):
... return datetime.datetime(2000,1,2,3,4,5,678).timetuple()
...
>>> formatter.converter = sim_time
>>> log = logging.getLogger('test')
>>> log.addHandler(handler)
>>> log.info('hi')
2000-01-02 03:04:05,898:hi
>>> log.info('hi')
2000-01-02 03:04:05,914:hi
>>> log.info('hi')
2000-01-02 03:04:05,434:hi
Here's a better example that allows you to replace the time that was generated, as the accepted answer didn't really do that.
To enable it:
override
logging.Formatter.formatTime()
instead with this:If you need it for all loggers in this process, you can override the class function itself, but do this right after the first
import logging
statement your code encounters:timetuple()
doesn't use milliseconds, so that the ms information contained in the datetime object is lost once the method is called:Note that this is not a limitation of this particular method, but rather of the
time.struct_time
type.The bottom line is: if you need to override the timestamp, don't pass through a
time.struct_time
object. You could - for example - pass the timestamp already formatted as a string, rather than a fake time. Depending on your needs there might be better methods, of course!