Skip to content

Commit 52fda5b

Browse files
committed
Add support for an empty msg dict
1 parent 13d8f1c commit 52fda5b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

logfmter/formatter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ def format(self, record: logging.LogRecord) -> str:
126126
extra = self.get_extra(record)
127127
params = {"msg": record.getMessage(), **extra}
128128

129-
tokens = ["at={}".format(record.levelname), self.format_params(params)]
129+
tokens = ["at={}".format(record.levelname)]
130+
131+
formatted_params = self.format_params(params)
132+
if formatted_params:
133+
tokens.append(formatted_params)
130134

131135
if record.exc_info:
132136
# Cast exc_info to its not null variant to make mypy happy.

tests/test_formatter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def test_get_extra(record, expected):
111111
},
112112
'at=INFO msg=alpha exc_info="Exception: alpha"',
113113
),
114+
# If, for some odd reason, someone passes in an empty msg dictionary. It
115+
# should be properly formatted without extra spaces.
116+
({"levelname": "INFO", "msg": {}}, "at=INFO"),
114117
],
115118
)
116119
def test_format(record, expected):

0 commit comments

Comments
 (0)