This is the representation desired for dates:
>>> tz = pytz.timezone('US/Central')
>>> datefmt = '%Y-%m-%d %H:%M:%S.%f%z(%Z)'
>>> datetime.now(tz).strftime(datefmt)
'2017-04-27 15:09:59.606921-0500(CDT)'
This is how it's logged (Python 3.6.0 on Linux):
>>> logrecord_format = '%(asctime)s %(levelname)s %(message)s'
>>> logging.basicConfig(format=logrecord_format, datefmt=datefmt)
>>> logging.error('ruh-roh!')
2017-04-27 15:10:35.%f-0500(CDT) ERROR ruh-roh!
It's not filling the microseconds properly. I've tried changing the logrecord_format
to a few other things, but I could not figure it out - how to configure the logger to show microseconds and timezone in the correct way to match the strftime
output exactly?
Edit: I could settle for milliseconds with offset, i.e. 2017-04-27 15:09:59,606-0500(CDT)
. Is that possible? logging
provides %(msecs)03d
directive, but I can't seem to get the timezone offset to appear after the milliseconds.