Python 2 logging module doesn’t support using a timezone offset %z in the datetime format string. An example failure mode is shown below:
import logging
datefmt = '%Y-%m-%d %H:%M:%S%z'
logging.basicConfig(format='%(asctime)s %(message)s', datefmt=datefmt)
logging.warning("Dude, where's my tzinfo?") # it's about 6.20 pm here in Chicago
# 2017-02-27 18:23:05+0000 Dude, where's my tzinfo?
^^^^^
Simply omitting the time zone offset would perhaps have been admissible, or perhaps even simply refusing to localize and just logging in UTC, but specifying the localized time and specifying a zero offset is certainly wrong. The part marked ^ is incorrect, an offset timestamp should have looked more like this one:
import pytz
from datetime import datetime
chicago_now = datetime.now(tz=pytz.timezone('America/Chicago'))
print chicago_now.strftime(datefmt)
# 2017-02-27 18:23:13-0600
Indeed, that’s how the logging output does look if run under Python 3. But in Python 2, the %z directive is dropped. This module provides a LocalFormatter intended as a drop-in replacement, to provide the correct handling of time zone offsets under Python 2:
import logging, logging_tz
datefmt = '%Y-%m-%d %H:%M:%S%z'
logger = logging.getLogger('wat')
handler = logging.StreamHandler()
formatter = logging_tz.LocalFormatter(fmt='%(asctime)s %(message)s', datefmt=datefmt)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.warning("Ah, there's my tzinfo!")
# 2017-02-27 18:25:53-0600 Ah, there's my tzinfo!
Additionally a ChicagoFormatter is offered as a convenience, to ease the pain for anyone insane enough to run their backend on CST instead of UTC.
FAQ
- Q:
How to install this library?
- A:
pip install logging_tz and then you can just use a logging_tz.LocalFormatter instead of the logging.Formatter.
- Q:
You should log in UTC. Why would you log in local time anyway?
- A:
Yeah, I guess. Although handling the date format correctly is at least better than mucking it up completely.
- Q:
Is that a bug in Python?
- A:
Hmm, arguably not, because the %z directive for time zone offset is not listed on the time.strftime table in the Python 2 documentation. It is there in the Python 3 docs, though.
- Q:
Did people really ask you these questions? I bet you just made them up for the FAQ.
- A:
That’s a very good question - it’s one I’ve frequently asked myself, in fact.
Metadata
Release files for logging-tz 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| logging_tz-0.1-py2-none-any.whl | Python 2 | none | any | Details |
Release files / logging_tz-0.1-py2-none-any.whl
| Download URL | logging_tz-0.1-py2-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
18a2c2cef418278153cd2710725279744933944291099c0c7670cc6c370f6fd8
|
|
BLAKE2b-256 checksum How to use checksums |
021f9d50b07253935e5af89b725780284e98adfbb9274d92a58f1f92c4699844
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |