Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert: "fix: JsonFormatter datetime format to include milliseconds (#…
…156)"

This reverts commit e47775d.
  • Loading branch information
darklight3it committed Jun 25, 2026
commit 4cddb14dc3fa2922559bdb44c34147e37376c3ea
7 changes: 1 addition & 6 deletions awslambdaric/lambda_runtime_log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import json
import logging
import traceback
from datetime import datetime, timezone
from enum import IntEnum

_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
_RESERVED_FIELDS = {
"name",
"msg",
Expand Down Expand Up @@ -82,10 +81,6 @@ class JsonFormatter(logging.Formatter):
def __init__(self):
super().__init__(datefmt=_DATETIME_FORMAT)

def formatTime(self, record, datefmt=None):
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
return dt.strftime(_DATETIME_FORMAT + ".%f")[:-3] + "Z"

@staticmethod
def __format_stacktrace(exc_info):
if not exc_info:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ def tearDownClass(cls):
def test_handler_setup(self, *_):
test_cases = [
(62, 0xA55A0003, 46, {}),
(137, 0xA55A001A, 121, {"AWS_LAMBDA_LOG_FORMAT": "JSON"}),
(133, 0xA55A001A, 117, {"AWS_LAMBDA_LOG_FORMAT": "JSON"}),
(62, 0xA55A001B, 46, {"AWS_LAMBDA_LOG_LEVEL": "INFO"}),
]

Expand Down
28 changes: 15 additions & 13 deletions tests/test_lambda_runtime_log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setUp(self):
self.logger = logging.getLogger("test")
self.logger.setLevel(logging.INFO)

def test_timestamp_includes_milliseconds(self):
def test_timestamp_format_is_second_precision_with_z(self):
record = logging.LogRecord(
name="test",
level=logging.INFO,
Expand All @@ -32,14 +32,14 @@ def test_timestamp_includes_milliseconds(self):
log_entry = json.loads(output)
timestamp = log_entry["timestamp"]

pattern = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$"
pattern = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$"
self.assertRegex(
timestamp,
pattern,
f"Timestamp '{timestamp}' does not match expected format YYYY-MM-DDTHH:MM:SS.mmmZ",
f"Timestamp '{timestamp}' does not match expected format YYYY-MM-DDTHH:MM:SSZ",
)

def test_timestamp_milliseconds_are_accurate(self):
def test_timestamp_value_is_accurate(self):
record = logging.LogRecord(
name="test",
level=logging.INFO,
Expand All @@ -53,9 +53,12 @@ def test_timestamp_milliseconds_are_accurate(self):
output = self.formatter.format(record)
log_entry = json.loads(output)

self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.068Z")
expected = time.strftime(
"%Y-%m-%dT%H:%M:%SZ", self.formatter.converter(record.created)
)
self.assertEqual(log_entry["timestamp"], expected)

def test_timestamp_zero_milliseconds(self):
def test_timestamp_does_not_include_milliseconds(self):
record = logging.LogRecord(
name="test",
level=logging.INFO,
Expand All @@ -65,13 +68,14 @@ def test_timestamp_zero_milliseconds(self):
args=None,
exc_info=None,
)
record.created = 1718838785.0
record.created = 1718838785.999
output = self.formatter.format(record)
log_entry = json.loads(output)

self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.000Z")
self.assertNotIn(".", log_entry["timestamp"])
self.assertTrue(log_entry["timestamp"].endswith("Z"))

def test_timestamps_differ_within_same_second(self):
def test_timestamps_same_within_same_second(self):
record1 = logging.LogRecord(
name="test",
level=logging.INFO,
Expand All @@ -92,11 +96,9 @@ def test_timestamps_differ_within_same_second(self):
args=None,
exc_info=None,
)
record2.created = 1718838785.200
record2.created = 1718838785.900

output1 = json.loads(self.formatter.format(record1))
output2 = json.loads(self.formatter.format(record2))

self.assertNotEqual(output1["timestamp"], output2["timestamp"])
self.assertEqual(output1["timestamp"], "2024-06-19T23:13:05.100Z")
self.assertEqual(output2["timestamp"], "2024-06-19T23:13:05.200Z")
self.assertEqual(output1["timestamp"], output2["timestamp"])