I'm running into an issue with Python 2.7 logging module. My system is Ubuntu 14.04 64bit, and I live in Italy (currently UTC+1, no daylight saving); the system is properly configured as such.
I want to emit logging lines in the current timezone, complete of correct timezone offset information.
Consider the following snippet:
#!/usr/bin/env python
import sys
print sys.version_info
import commands
print "System time: %s" % commands.getoutput("date --rfc-3339=seconds")
import logging
import datetime
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format="%(asctime)s:" + logging.BASIC_FORMAT,
datefmt="%Y-%m-%dT%H:%M:%S%z")
logger = logging.getLogger()
logger.info("Something happened")
Which yields this result:
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
System time: 2015-01-09 11:21:44+01:00
2015-01-09T11:21:44+0000:INFO:root:Something happened
So, the system knows the proper time AND offset, while Python seems to get the time wrong.
In datetime docs, it is said that for %z in strftime the result is"UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive)".
So, I would have expected either of the following results:
- The logging module wants to log in UTC, so I would get something like 10:21.44 and a +0000 offset;
- The logging module wants to log in local time, so I would get something like 11:21:44 and a +01000 offset;
- The logging module offers naive datetime objects, and we get absolutely nothing as on offset.
On the contrary, here I seem to get an unpredictable - and plainly wrong - result. What's up? Is it possible to configure the logging module to do what I want WITHOUT overriding the converter function of logging.Formatter objects (where, of course, I can do whatever I like, but this looks plainly a bug to me)?
pytz
is a good source, but I don't know if you can propagate that intologging
. – Hypomania