Skip to content

Timezone aware timestamp logging #379

Closed

Description

@bml1g12

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?

Activity

  1. changed the title [-]Timezone logigng[/-] [+]Timezone aware timestamp logging[/+] on Apr 2, 2021
  2. heitorlessa commented on Apr 2, 2021

    @heitorlessa
    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

  3. heitorlessa commented on Apr 3, 2021

    @heitorlessa
    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}
  4. self-assigned this
    on Apr 3, 2021
  5. bml1g12 commented on Apr 4, 2021

    @bml1g12
    Author

    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

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions