Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 0055dc5

Browse files
authored
Adding support for all string formatting styles: '%', '$', or '{'.
1 parent e228788 commit 0055dc5

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/pythonjsonlogger/jsonlogger.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,18 @@ def parse(self):
142142
143143
This method is responsible for returning a list of fields (as strings)
144144
to include in all log messages.
145-
"""
146-
standard_formatters = re.compile(r'\((.+?)\)', re.IGNORECASE)
147-
return standard_formatters.findall(self._fmt)
145+
"""
146+
if isinstance(self._style, logging.StringTemplateStyle):
147+
formatter_style_pattern = re.compile(r'\$\{(.+?)\}', re.IGNORECASE)
148+
elif isinstance(self._style, logging.StrFormatStyle):
149+
formatter_style_pattern = re.compile(r'\{(.+?)\}', re.IGNORECASE)
150+
# PercentStyle is parent class of StringTemplateStyle and StrFormatStyle so
151+
# it needs to be checked last.
152+
elif isinstance(self._style, logging.PercentStyle):
153+
formatter_style_pattern = re.compile(r'%\((.+?)\)s', re.IGNORECASE)
154+
else:
155+
raise ValueError('Invalid format: %s' % self._fmt)
156+
return formatter_style_pattern.findall(self._fmt)
148157

149158
def add_fields(self, log_record, record, message_dict):
150159
"""

0 commit comments

Comments
 (0)