{ "info": { "author": "Wim Glenn", "author_email": "hey@wimglenn.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2 :: Only", "Topic :: Software Development :: Libraries", "Topic :: System :: Logging" ], "description": "Using local time in Python2 logging\n===================================\n\nPython 2 logging module doesn't support using a timezone offset ``%z`` in the datetime format string. An example failure mode is shown below:\n\n.. code-block:: python\n\n import logging\n datefmt = '%Y-%m-%d %H:%M:%S%z'\n logging.basicConfig(format='%(asctime)s %(message)s', datefmt=datefmt)\n logging.warning(\"Dude, where's my tzinfo?\") # it's about 6.20 pm here in Chicago\n # 2017-02-27 18:23:05+0000 Dude, where's my tzinfo?\n ^^^^^\n\nSimply 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:\n\n.. code-block:: python\n\n import pytz\n from datetime import datetime\n chicago_now = datetime.now(tz=pytz.timezone('America/Chicago'))\n print chicago_now.strftime(datefmt)\n # 2017-02-27 18:23:13-0600\n\nIndeed, 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:\n\n.. code-block:: python\n\n import logging, logging_tz\n datefmt = '%Y-%m-%d %H:%M:%S%z'\n logger = logging.getLogger('wat')\n handler = logging.StreamHandler()\n formatter = logging_tz.LocalFormatter(fmt='%(asctime)s %(message)s', datefmt=datefmt)\n handler.setFormatter(formatter)\n logger.addHandler(handler)\n logger.warning(\"Ah, there's my tzinfo!\")\n # 2017-02-27 18:25:53-0600 Ah, there's my tzinfo!\n\nAdditionally a ``ChicagoFormatter`` is offered as a convenience, to ease the pain for anyone insane enough to run their backend on CST instead of UTC.\n\n\nFAQ\n---\n\nQ:\n How to install this library?\nA:\n ``pip install logging_tz`` and then you can just use a ``logging_tz.LocalFormatter`` instead of the ``logging.Formatter``.\n\n\nQ:\n You should log in UTC. Why would you log in local time anyway?\nA:\n `Yeah, I guess `_. Although handling the date format correctly is at least better than mucking it up completely.\n\n\nQ:\n Is that a bug in Python?\nA:\n 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.\n\n\nQ:\n Did people really ask you these questions? I bet you just made them up for the FAQ.\nA:\n That's a very good question - it's one I've frequently asked myself, in fact.\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/wimglenn/logging_tz", "keywords": "logging,datetime,timezone", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "logging-tz", "package_url": "https://pypi.org/project/logging-tz/", "platform": "", "project_url": "https://pypi.org/project/logging-tz/", "project_urls": { "Homepage": "https://github.com/wimglenn/logging_tz" }, "release_url": "https://pypi.org/project/logging-tz/0.1/", "requires_dist": [ "pytz", "tzlocal" ], "requires_python": "<3", "summary": "Specify the UTC offset in Python 2 logging", "version": "0.1" }, "last_serial": 3389359, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "14c613d1a02dd64c2cf99bba2616d756", "sha256": "18a2c2cef418278153cd2710725279744933944291099c0c7670cc6c370f6fd8" }, "downloads": -1, "filename": "logging_tz-0.1-py2-none-any.whl", "has_sig": false, "md5_digest": "14c613d1a02dd64c2cf99bba2616d756", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": "<3", "size": 5716, "upload_time": "2017-12-04T05:17:25", "url": "https://files.pythonhosted.org/packages/02/1f/9d50b07253935e5af89b725780284e98adfbb9274d92a58f1f92c4699844/logging_tz-0.1-py2-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "14c613d1a02dd64c2cf99bba2616d756", "sha256": "18a2c2cef418278153cd2710725279744933944291099c0c7670cc6c370f6fd8" }, "downloads": -1, "filename": "logging_tz-0.1-py2-none-any.whl", "has_sig": false, "md5_digest": "14c613d1a02dd64c2cf99bba2616d756", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": "<3", "size": 5716, "upload_time": "2017-12-04T05:17:25", "url": "https://files.pythonhosted.org/packages/02/1f/9d50b07253935e5af89b725780284e98adfbb9274d92a58f1f92c4699844/logging_tz-0.1-py2-none-any.whl" } ] }