Skip to main content

Specify the UTC offset in Python 2 logging

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

logging_tz-0.1-py2-none-any.whl (5.7 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page