diff --git a/CHANGELOG.md b/CHANGELOG.md index 891a970..8861223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.4.3 (2024-07-15) + +- Keep any trailing whitespace passed into SyntaxHighlighter + ## 0.4.2 (2024-07-15) - Fix typing import unavailable on 3.8 diff --git a/setup.py b/setup.py index b7c73ba..fdb4d65 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ setup( name="structlog-pretty", - version="0.4.2", + version="0.4.3", url="https://github.com/underyx/structlog-pretty", author="Bence Nagy", author_email="bence@underyx.me", diff --git a/structlog_pretty/processors.py b/structlog_pretty/processors.py index 706ae2e..697bab3 100644 --- a/structlog_pretty/processors.py +++ b/structlog_pretty/processors.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, print_function from pathlib import Path +import re import sys import json @@ -178,9 +179,19 @@ def __call__(self, _, __, event_dict): code = event_dict[field] except KeyError: continue + if not code: continue - event_dict[field] = highlight(code, lexer, TerminalFormatter()).rstrip() + + trailing_whitespace_match = re.search(r"\s*$", code) + trailing_whitespace = ( + trailing_whitespace_match.group(0) if trailing_whitespace_match else "" + ) + + event_dict[field] = ( + highlight(code, lexer, TerminalFormatter()).rstrip() + + trailing_whitespace + ) return event_dict diff --git a/test/test_SyntaxHighlighter.py b/test/test_SyntaxHighlighter.py index 71a7bef..5af3272 100644 --- a/test/test_SyntaxHighlighter.py +++ b/test/test_SyntaxHighlighter.py @@ -1,3 +1,4 @@ +import re from structlog_pretty.processors import SyntaxHighlighter as uut @@ -10,6 +11,14 @@ def test_json(): ), "should not have trailing newline added" +def test_retain_whitespace(): + processor = uut(field_map={"body": "json"}) + event_dict = processor(None, None, {"body": '{"ping": true}\n\n'}) + match = re.search(r"\s*$", event_dict["body"]) + assert match is not None + assert match.group() == "\n\n" + + def test_missing_json(): processor = uut(field_map={"body": "json"}) event_dict = processor(None, None, {"not_body": '{"ping": true}'})