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

Commit 2078d26

Browse files
authored
Merge pull request #117 from Alsheh/patch-5
Adding support for all string formatting styles: '%', '$', or '{'.
2 parents d710af9 + 0055dc5 commit 2078d26

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
@@ -144,9 +144,18 @@ def parse(self):
144144
145145
This method is responsible for returning a list of fields (as strings)
146146
to include in all log messages.
147-
"""
148-
standard_formatters = re.compile(r'\((.+?)\)', re.IGNORECASE)
149-
return standard_formatters.findall(self._fmt)
147+
"""
148+
if isinstance(self._style, logging.StringTemplateStyle):
149+
formatter_style_pattern = re.compile(r'\$\{(.+?)\}', re.IGNORECASE)
150+
elif isinstance(self._style, logging.StrFormatStyle):
151+
formatter_style_pattern = re.compile(r'\{(.+?)\}', re.IGNORECASE)
152+
# PercentStyle is parent class of StringTemplateStyle and StrFormatStyle so
153+
# it needs to be checked last.
154+
elif isinstance(self._style, logging.PercentStyle):
155+
formatter_style_pattern = re.compile(r'%\((.+?)\)s', re.IGNORECASE)
156+
else:
157+
raise ValueError('Invalid format: %s' % self._fmt)
158+
return formatter_style_pattern.findall(self._fmt)
150159

151160
def add_fields(self, log_record, record, message_dict):
152161
"""

0 commit comments

Comments
 (0)