-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: emit openjd_* log messages without any log formatting (#16)
Signed-off-by: Jericho Tolentino <[email protected]>
- Loading branch information
Showing
4 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,37 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
import logging | ||
import re | ||
from typing import ( | ||
List, | ||
Optional, | ||
) | ||
|
||
_OPENJD_LOG_PATTERN = r"^openjd_\S+: " | ||
_OPENJD_LOG_REGEX = re.compile(_OPENJD_LOG_PATTERN) | ||
|
||
|
||
class ConditionalFormatter(logging.Formatter): | ||
""" | ||
A Formatter subclass that applies formatting conditionally. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
ignore_patterns: Optional[List[re.Pattern[str]]], | ||
**kwargs, | ||
): | ||
""" | ||
Args: | ||
ignore_patterns (Optional[List[re.Pattern[str]]]): List of patterns that, when matched, | ||
indicate a log message must not be formatted (it is "ignored" by the formatter) | ||
""" | ||
self._ignore_patterns = ignore_patterns or [] | ||
super().__init__(*args, **kwargs) | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
for ignore_pattern in self._ignore_patterns: | ||
if ignore_pattern.match(record.msg): | ||
return record.getMessage() | ||
|
||
return super().format(record) |
This file contains 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 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,57 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
import logging | ||
import re | ||
from typing import List | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
import openjd.adaptor_runtime._utils._logging as logging_mod | ||
from openjd.adaptor_runtime._utils._logging import ConditionalFormatter | ||
|
||
|
||
class TestConditionalFormatter: | ||
@pytest.mark.parametrize( | ||
["patterns", "message", "should_be_ignored"], | ||
[ | ||
[ | ||
[ | ||
re.compile(r"^IGNORE:"), | ||
], | ||
"IGNORE: This should be ignored", | ||
True, | ||
], | ||
[ | ||
[re.compile(r"^IGNORE:"), re.compile(r"^IGNORE_TWO:")], | ||
"IGNORE_TWO: This should also be ignored", | ||
True, | ||
], | ||
[ | ||
[ | ||
re.compile(r"^IGNORE:"), | ||
], | ||
"INFO: This should not be ignored", | ||
False, | ||
], | ||
], | ||
) | ||
def test_ignores_patterns( | ||
self, | ||
patterns: List[re.Pattern[str]], | ||
message: str, | ||
should_be_ignored: bool, | ||
) -> None: | ||
# GIVEN | ||
record = logging.LogRecord("NAME", 0, "", 0, message, None, None) | ||
formatter = ConditionalFormatter(ignore_patterns=patterns) | ||
|
||
# WHEN | ||
with patch.object(logging_mod.logging.Formatter, "format") as mock_format: | ||
formatter.format(record) | ||
|
||
# THEN | ||
if should_be_ignored: | ||
mock_format.assert_not_called() | ||
else: | ||
mock_format.assert_called_once_with(record) |