-
Notifications
You must be signed in to change notification settings - Fork 82
fix: timestamp format in logs #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxday
wants to merge
3
commits into
main
Choose a base branch
from
maxday/fix-156
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """ | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
|
|
||
| import logging | ||
| import json | ||
| import re | ||
| import time | ||
| import unittest | ||
|
|
||
| from awslambdaric.lambda_runtime_log_utils import JsonFormatter | ||
|
|
||
|
|
||
| class TestJsonFormatterTimestamp(unittest.TestCase): | ||
| def setUp(self): | ||
| self.formatter = JsonFormatter() | ||
| self.logger = logging.getLogger("test") | ||
| self.logger.setLevel(logging.INFO) | ||
|
|
||
| def test_timestamp_includes_milliseconds(self): | ||
| record = logging.LogRecord( | ||
| name="test", | ||
| level=logging.INFO, | ||
| pathname="test.py", | ||
| lineno=1, | ||
| msg="hello", | ||
| args=None, | ||
| exc_info=None, | ||
| ) | ||
| output = self.formatter.format(record) | ||
| 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$" | ||
| self.assertRegex( | ||
| timestamp, | ||
| pattern, | ||
| f"Timestamp '{timestamp}' does not match expected format YYYY-MM-DDTHH:MM:SS.mmmZ", | ||
| ) | ||
|
|
||
| def test_timestamp_milliseconds_are_accurate(self): | ||
| record = logging.LogRecord( | ||
| name="test", | ||
| level=logging.INFO, | ||
| pathname="test.py", | ||
| lineno=1, | ||
| msg="hello", | ||
| args=None, | ||
| exc_info=None, | ||
| ) | ||
| record.created = 1718838785.068 | ||
| output = self.formatter.format(record) | ||
| log_entry = json.loads(output) | ||
|
|
||
| self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.068Z") | ||
|
|
||
| def test_timestamp_zero_milliseconds(self): | ||
| record = logging.LogRecord( | ||
| name="test", | ||
| level=logging.INFO, | ||
| pathname="test.py", | ||
| lineno=1, | ||
| msg="hello", | ||
| args=None, | ||
| exc_info=None, | ||
| ) | ||
| record.created = 1718838785.0 | ||
| output = self.formatter.format(record) | ||
| log_entry = json.loads(output) | ||
|
|
||
| self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.000Z") | ||
|
|
||
| def test_timestamps_differ_within_same_second(self): | ||
| record1 = logging.LogRecord( | ||
| name="test", | ||
| level=logging.INFO, | ||
| pathname="test.py", | ||
| lineno=1, | ||
| msg="first", | ||
| args=None, | ||
| exc_info=None, | ||
| ) | ||
| record1.created = 1718838785.100 | ||
|
|
||
| record2 = logging.LogRecord( | ||
| name="test", | ||
| level=logging.INFO, | ||
| pathname="test.py", | ||
| lineno=1, | ||
| msg="second", | ||
| args=None, | ||
| exc_info=None, | ||
| ) | ||
| record2.created = 1718838785.200 | ||
|
|
||
| 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") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used by any other code? I nothing else is using it we can just append
.%fhere.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be safer to leave that string and just use another format string in the
formatTimeoverride