You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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
fromaws_lambda_powertoolsimportLoggerfromaws_lambda_powertools.logging.formatterimportJsonFormatterimporttimelogger=Logger(service="pt_issue_379")
logger.info("Local time")
logger._logger.handlers[0].formatter.converter=time.gmtime# as per official Python docslogger.info("GMT time")
Setting GMT time via monkeypatching
fromaws_lambda_powertoolsimportLoggerfromaws_lambda_powertools.logging.formatterimportJsonFormatterimporttimeJsonFormatter.converter=time.gmtime# monkeypatch `converter` Formatter method before creating a logger instancelogger=Logger(service="pt_issue_379")
logger.info("Local time")
logger.info("GMT time")
Activity
[-]Timezone logigng[/-][+]Timezone aware timestamp logging[/+]heitorlessa commentedon Apr 2, 2021
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 commentedon Apr 3, 2021
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
Setting GMT time via monkeypatching
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}bml1g12 commentedon 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.