From 369975ab9912ec2fb9ed4aec094c3ec8815df35e Mon Sep 17 00:00:00 2001 From: Stefan Rom Date: Fri, 12 May 2023 13:56:33 +0200 Subject: [PATCH] Don't stringify stringify_fields contint if --- sag_py_logging/console_extra_field_filter.py | 1 + tests/test_console_extra_field_filter.py | 25 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/sag_py_logging/console_extra_field_filter.py b/sag_py_logging/console_extra_field_filter.py index 5b3fca5..c7ebf8d 100644 --- a/sag_py_logging/console_extra_field_filter.py +++ b/sag_py_logging/console_extra_field_filter.py @@ -37,6 +37,7 @@ def __init__(self, name: str = "") -> None: "tags", "@metadata", "color_message", + "stringified_extra", } def filter(self, record: logging.LogRecord) -> bool: diff --git a/tests/test_console_extra_field_filter.py b/tests/test_console_extra_field_filter.py index d06db09..a40d7dc 100644 --- a/tests/test_console_extra_field_filter.py +++ b/tests/test_console_extra_field_filter.py @@ -46,3 +46,28 @@ def test_with_extra_fields(log_record: LogRecord) -> None: 'my_extra_not_serializable_object={"testtext": "test"}, ' "my_extra_object_without_dict=ClassWithoutDict(x=123, y=456)" ) + + +def test_with_extra_fields_when_called_twice(log_record: LogRecord) -> None: + # Arrange + filter_ = ConsoleExtraFieldFilter() + log_record.my_extra_string = "test" + log_record.my_extra_int = 1 + log_record.my_extra_bool = True + log_record.my_extra_dict_object = {"keyOne": "valueOne", "keyTwo": 2} + log_record.my_extra_not_serializable_object = NotSerializableClass("test") + log_record.my_extra_object_without_dict = ClassWithoutDict(x=123, y=456) + + # Act + filter_.filter(log_record) + filter_.filter(log_record) + + # Assert + assert ( + cast(ExtraFieldsLogRecord, log_record).stringified_extra == 'my_extra_string="test", ' + "my_extra_int=1, " + "my_extra_bool=true, " + 'my_extra_dict_object={"keyOne": "valueOne", "keyTwo": 2}, ' + 'my_extra_not_serializable_object={"testtext": "test"}, ' + "my_extra_object_without_dict=ClassWithoutDict(x=123, y=456)" + )