Skip to content

Commit 0b9592c

Browse files
authored
fix: quote newlines to distinguish from escaped newline strings
Resolves #49
1 parent 949038b commit 0b9592c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/logfmter/formatter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ def format_string(cls, value: str) -> str:
6060
"""
6161
needs_dquote_escaping = '"' in value
6262
needs_newline_escaping = "\n" in value
63-
needs_quoting = " " in value or "=" in value or needs_dquote_escaping
63+
needs_quoting = (
64+
" " in value
65+
or "=" in value
66+
or needs_dquote_escaping
67+
or needs_newline_escaping
68+
)
6469
needs_backslash_escaping = "\\" in value and needs_quoting
6570

6671
if needs_backslash_escaping:

tests/test_formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
# If the string is empty, then it should be left empty.
2323
("", ""),
2424
# If the string contains a newline, then it should be escaped.
25-
("\n", "\\n"),
26-
("\n\n", "\\n\\n"),
25+
("\n", '"\\n"'),
26+
("\n\n", '"\\n\\n"'),
2727
# If the string contains a backslash and needs to be quoted, then
2828
# the backslashes need to be escaped.
2929
("\\", "\\"),

0 commit comments

Comments
 (0)