-
Notifications
You must be signed in to change notification settings - Fork 421
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
Comments
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 |
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 instancefrom 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 monkeypatchingfrom 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} |
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. |
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?
The text was updated successfully, but these errors were encountered: