Skip to content

Bug: Logger timestamp is off by up to one second when use_datetime_directive=True #8631

Closed

Description

@sebxt

Expected Behaviour

With use_datetime_directive=True, the timestamp key should show the time the log record was created, the same value the default formatting path shows.

Current Behaviour

The timestamp is shifted forward by the record's milliseconds. Whenever the sub second part is 500 ms or more, the seconds field is one second ahead, and %f shows the wrong microseconds.

For a record created at 1700000000.75 (17:13:20.750 UTC):

datefmt="%H:%M:%S.%F", use_datetime_directive=True  -> 17:13:21.750
datefmt="%H:%M:%S.%F"                                -> 17:13:20.750

Code snippet

import logging
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter

formatter = LambdaPowertoolsFormatter(datefmt="%H:%M:%S.%F", use_datetime_directive=True, utc=True)
record = logging.LogRecord("x", logging.INFO, "p", 1, "m", None, None)
record.created = 1700000000.75
record.msecs = 750.0

print(formatter.formatTime(record, formatter.datefmt))  # 17:13:21.750, expected 17:13:20.750

Possible Solution

In LambdaPowertoolsFormatter.formatTime, the datetime directive branch builds the datetime from record.created + record.msecs / 1000. The comment says record.msecs is in microseconds, but it is the millisecond part of record.created (0 to 999), and record.created already includes the fractional second. The milliseconds are counted twice. Using record.created directly fixes it.

Steps to Reproduce

Run the snippet above, or log with Logger(datefmt="%H:%M:%S.%F", use_datetime_directive=True) and compare against the default timestamp.

Powertools for AWS Lambda (Python) version

latest (develop at 0f64f28)

AWS Lambda function runtime

3.12

Activity

  1. boring-cyborg commented on Oct 19, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  2. powertools-for-aws-oss-automation commented on Oct 19, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

  3. added this to the 3.38.0 milestone on Oct 22, 2026
  4. powertools-for-aws-oss-automation commented on Oct 23, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  5. added
    pending-releaseFix or implementation already in dev waiting to be released
    and removed
    triagePending triage from maintainers
    on Oct 23, 2026
  6. github-actions commented on Oct 28, 2026

    @github-actions
    Contributor

    This is now released under 3.38.0 version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglogger

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions