Skip to content

Commit 72e9060

Browse files
committed
fix: fix quoting for backslash escaping
1 parent a394264 commit 72e9060

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

src/logfmter/formatter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ def format_string(cls, value: str) -> str:
6060
"""
6161
needs_dquote_escaping = '"' in value
6262
needs_newline_escaping = "\n" in value
63+
needs_backslash_escaping = "\\" in value
6364
needs_quoting = (
6465
" " in value
6566
or "=" in value
6667
or needs_dquote_escaping
6768
or needs_newline_escaping
69+
or needs_backslash_escaping
6870
)
69-
needs_backslash_escaping = "\\" in value and needs_quoting
7071

7172
if needs_backslash_escaping:
7273
value = value.replace("\\", "\\\\")

tests/test_formatter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
("\n\n", '"\\n\\n"'),
2727
# If the string contains a backslash and needs to be quoted, then
2828
# the backslashes need to be escaped.
29-
("\\", "\\"),
29+
("\\", r'"\\"'),
3030
("\\ ", '"\\\\ "'),
3131
('\\"', '"\\\\\\""'),
32+
("\\n", r'"\\n"'),
3233
]
3334

3435
TYPE_CONVERSION_RULES = [

0 commit comments

Comments
 (0)