Skip to content

Commit

Permalink
fix create formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
duyguHsnHsn committed Nov 30, 2023
1 parent 3dacb6e commit e67d945
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ def create_formatter(
key_set = set(metadata_keys.split(","))
formatter = None
custom_key_filter = None
if custom_console_format:
formatter = ConsoleFormatter(fmt=custom_console_format)
custom_key_filter = ConsoleMetadataFilter(key_set)
elif logging_format == "json":
if logging_format == "json":
formatter = JsonFormatter(
StructlogMetadataBuilder(metadata_keys).build_json_format()
)
Expand All @@ -157,7 +154,10 @@ def create_formatter(
fmt=StructlogMetadataBuilder(metadata_keys).build_ltsv_format()
)
custom_key_filter = ConsoleMetadataFilter(key_set)
else:
formatter = ConsoleFormatter(fmt=StructlogMetadataBuilder(metadata_keys).build_console_format())
elif logging_format == "console":
format_string = custom_console_format if custom_console_format else StructlogMetadataBuilder(
metadata_keys).build_console_format()
formatter = ConsoleFormatter(fmt=format_string)
custom_key_filter = ConsoleMetadataFilter(key_set)

return formatter, custom_key_filter
6 changes: 3 additions & 3 deletions projects/vdk-plugins/vdk-structlog/tests/test_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_custom_format_applied(log_format):


@pytest.mark.parametrize("log_format", ["json", "ltsv"])
def test_custom_format_overrides_default(log_format):
def test_custom_format_not_applied_for_non_console_formats(log_format):
custom_format_string = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"

with mock.patch.dict(
Expand All @@ -150,8 +150,8 @@ def test_custom_format_overrides_default(log_format):

for log in logs:
if "Log statement with no bound context" in log:
assert _matches_custom_format(log), \
f"Log format did not match the custom format. Log: {log}"
assert not _matches_custom_format(log), \
f"Custom format was incorrectly applied for {log_format} format. Log: {log}"
break
else:
pytest.fail("Log statement with no bound context not found in logs")
Expand Down

0 comments on commit e67d945

Please sign in to comment.