By default logging.Formatter('%(asctime)s')
prints with the following format:
JavaScript
x
2
1
2011-06-09 10:54:40,638
2
where 638 is the millisecond. I need to change the comma to a dot:
JavaScript
1
2
1
2011-06-09 10:54:40.638
2
To format the time I can use:
JavaScript
1
2
1
logging.Formatter(fmt='%(asctime)s',datestr=date_format_str)
2
however the documentation doesn’t specify how to format milliseconds. I’ve found this SO question which talks about microseconds, but a) I would prefer milliseconds and b) the following doesn’t work on Python 2.6 (which I’m working on) due to the %f
:
JavaScript
1
2
1
logging.Formatter(fmt='%(asctime)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
2
Advertisement
Answer
This should work too:
JavaScript
1
5
1
logging.Formatter(
2
fmt='%(asctime)s.%(msecs)03d',
3
datefmt='%Y-%m-%d,%H:%M:%S'
4
)
5