Skip to content

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

Open

Description

@auraseb

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions