Skip to content

Commit 4cddb14

Browse files
committed
revert: "fix: JsonFormatter datetime format to include milliseconds (#156)"
This reverts commit e47775d.
1 parent 389ec8c commit 4cddb14

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

awslambdaric/lambda_runtime_log_utils.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import json
77
import logging
88
import traceback
9-
from datetime import datetime, timezone
109
from enum import IntEnum
1110

12-
_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
11+
_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
1312
_RESERVED_FIELDS = {
1413
"name",
1514
"msg",
@@ -82,10 +81,6 @@ class JsonFormatter(logging.Formatter):
8281
def __init__(self):
8382
super().__init__(datefmt=_DATETIME_FORMAT)
8483

85-
def formatTime(self, record, datefmt=None):
86-
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
87-
return dt.strftime(_DATETIME_FORMAT + ".%f")[:-3] + "Z"
88-
8984
@staticmethod
9085
def __format_stacktrace(exc_info):
9186
if not exc_info:

tests/test_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ def tearDownClass(cls):
12511251
def test_handler_setup(self, *_):
12521252
test_cases = [
12531253
(62, 0xA55A0003, 46, {}),
1254-
(137, 0xA55A001A, 121, {"AWS_LAMBDA_LOG_FORMAT": "JSON"}),
1254+
(133, 0xA55A001A, 117, {"AWS_LAMBDA_LOG_FORMAT": "JSON"}),
12551255
(62, 0xA55A001B, 46, {"AWS_LAMBDA_LOG_LEVEL": "INFO"}),
12561256
]
12571257

tests/test_lambda_runtime_log_utils.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setUp(self):
1818
self.logger = logging.getLogger("test")
1919
self.logger.setLevel(logging.INFO)
2020

21-
def test_timestamp_includes_milliseconds(self):
21+
def test_timestamp_format_is_second_precision_with_z(self):
2222
record = logging.LogRecord(
2323
name="test",
2424
level=logging.INFO,
@@ -32,14 +32,14 @@ def test_timestamp_includes_milliseconds(self):
3232
log_entry = json.loads(output)
3333
timestamp = log_entry["timestamp"]
3434

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

42-
def test_timestamp_milliseconds_are_accurate(self):
42+
def test_timestamp_value_is_accurate(self):
4343
record = logging.LogRecord(
4444
name="test",
4545
level=logging.INFO,
@@ -53,9 +53,12 @@ def test_timestamp_milliseconds_are_accurate(self):
5353
output = self.formatter.format(record)
5454
log_entry = json.loads(output)
5555

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

58-
def test_timestamp_zero_milliseconds(self):
61+
def test_timestamp_does_not_include_milliseconds(self):
5962
record = logging.LogRecord(
6063
name="test",
6164
level=logging.INFO,
@@ -65,13 +68,14 @@ def test_timestamp_zero_milliseconds(self):
6568
args=None,
6669
exc_info=None,
6770
)
68-
record.created = 1718838785.0
71+
record.created = 1718838785.999
6972
output = self.formatter.format(record)
7073
log_entry = json.loads(output)
7174

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

74-
def test_timestamps_differ_within_same_second(self):
78+
def test_timestamps_same_within_same_second(self):
7579
record1 = logging.LogRecord(
7680
name="test",
7781
level=logging.INFO,
@@ -92,11 +96,9 @@ def test_timestamps_differ_within_same_second(self):
9296
args=None,
9397
exc_info=None,
9498
)
95-
record2.created = 1718838785.200
99+
record2.created = 1718838785.900
96100

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

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

0 commit comments

Comments
 (0)