Skip to content

Timezone aware timestamp logging #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bml1g12 opened this issue Apr 2, 2021 · 3 comments
Closed

Timezone aware timestamp logging #379

bml1g12 opened this issue Apr 2, 2021 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@bml1g12
Copy link

bml1g12 commented Apr 2, 2021

What were you initially searching for in the docs?
How to log the timezone in a timezone aware format ("2019-02-04T12:23:34Z" ISO 8601 ) e.g. to guarantee UTC time is always logged, even when logging in JST, or to at least indicate the time zone.

Describe how we could make it clearer

I can see how with this library we can specify a format string for the datetime, and I think datetime is hard coded to come from https://docs.python.org/3/library/time.html#time.asctime asctime() if I understand correctly, which in turn means that we are logging in time-zone unaware local time.

The documentation is unclear (if it is even possible) on how to log in a timezone aware manner.

They say here that "Python datetime objects don't have time zone info by default, and without it, Python actually violates the ISO 8601 specification (if no time zone info is given, assumed to be local time). You can use the pytz package to get some default time zones, or directly subclass tzinfo yourself"
https://stackoverflow.com/questions/19654578/python-utc-datetime-objects-iso-format-doesnt-include-z-zulu-or-zero-offset

So if I wanted to say log in strict ISO 8601 specification, or ensure my logs are always UTC no matter where the code is run, I am unclear how to do this?

@bml1g12 bml1g12 added the documentation Improvements or additions to documentation label Apr 2, 2021
@bml1g12 bml1g12 changed the title Timezone logigng Timezone aware timestamp logging Apr 2, 2021
@heitorlessa
Copy link
Contributor

Hey @bml1g12 thanks for raising this.

It's spot on Standard lib comment and your research. It can be a new feature for Logger to have a flag to convert to UTC or a given offset.

On an extended weekend now but you can take a peek at the formatter.py as that's where it should go.

Would you mind creating a feature request with a proposed UX for this?

We'd be happy to implement it

@heitorlessa
Copy link
Contributor

Got my laptop and managed to test this to unblock you for now. You can either change the time converter after creating a Logger or monkeypatch Powertools Logging Formatter - I personally find the former cleaner and less error prone, so I'll leave at your discretion.

Setting GMT time converter at Logger instance

from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.formatter import JsonFormatter

import time

logger = Logger(service="pt_issue_379")

logger.info("Local time")
logger._logger.handlers[0].formatter.converter = time.gmtime  # as per official Python docs
logger.info("GMT time")

Setting GMT time via monkeypatching

from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.formatter import JsonFormatter

import time

JsonFormatter.converter = time.gmtime  # monkeypatch `converter` Formatter method before creating a logger instance

logger = Logger(service="pt_issue_379")

logger.info("Local time")
logger.info("GMT time")

Results

{"level": "INFO", "location": "<module>:10", "message": "Local time", "timestamp": "2021-04-03 10:49:26,302", "service": "pt_issue_379", "sampling_rate": 0.0}

{"level": "INFO", "location": "<module>:14", "message": "GMT time", "timestamp": "2021-04-03 08:49:26,303", "service": "pt_issue_379", "sampling_rate": 0.0}

@heitorlessa heitorlessa self-assigned this Apr 3, 2021
@bml1g12
Copy link
Author

bml1g12 commented Apr 4, 2021

That sounds perfect! Thanks for the swift solution.

As you suggested, I'll open up a feature request for a flag for this; given I imagine it would be a common desired use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Development

No branches or pull requests

2 participants