From a0e35386062a47816a14e230f40be716964d4c70 Mon Sep 17 00:00:00 2001 From: Georges Dubus Date: Mon, 29 Jan 2024 11:30:17 +0100 Subject: [PATCH 1/2] Escape newlines in LogfmtRenderer --- CHANGELOG.md | 4 ++++ src/structlog/processors.py | 2 +- tests/test_processors.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d8f2b87..20cacf5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/ ## [Unreleased](https://github.com/hynek/structlog/compare/24.1.0...HEAD) +### Changed + +- `structlog.processors.LogfmtRenderer` now escapes newlines. + [#592](https://github.com/hynek/structlog/pull/592) ## [24.1.0](https://github.com/hynek/structlog/compare/23.3.0...24.1.0) - 2024-01-08 diff --git a/src/structlog/processors.py b/src/structlog/processors.py index 3f25dfff..b7c78055 100644 --- a/src/structlog/processors.py +++ b/src/structlog/processors.py @@ -170,7 +170,7 @@ def __call__( continue value = "true" if value else "false" - value = f"{value}".replace('"', '\\"') + value = f"{value}".replace('"', '\\"').replace("\n", "\\n") if " " in value or "=" in value: value = f'"{value}"' diff --git a/tests/test_processors.py b/tests/test_processors.py index 04eb6a85..5c09fea1 100644 --- a/tests/test_processors.py +++ b/tests/test_processors.py @@ -290,6 +290,14 @@ def test_invalid_key(self): with pytest.raises(ValueError, match='Invalid key: "invalid key"'): LogfmtRenderer()(None, None, event_dict) + def test_newline_in_value(self): + """ + Newlines in values should be escaped + """ + event_dict = {"with_newline": "some\nvalue"} + rv = LogfmtRenderer()(None, None, event_dict) + assert r"with_newline=some\nvalue" == rv + class TestJSONRenderer: def test_renders_json(self, event_dict): From 7a0f77b3fbadeb500fcba06f1998591c47c0da19 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Mon, 29 Jan 2024 16:47:38 +0100 Subject: [PATCH 2/2] Update tests/test_processors.py --- tests/test_processors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_processors.py b/tests/test_processors.py index 5c09fea1..9fd16787 100644 --- a/tests/test_processors.py +++ b/tests/test_processors.py @@ -292,10 +292,12 @@ def test_invalid_key(self): def test_newline_in_value(self): """ - Newlines in values should be escaped + Newlines in values are escaped. """ event_dict = {"with_newline": "some\nvalue"} + rv = LogfmtRenderer()(None, None, event_dict) + assert r"with_newline=some\nvalue" == rv