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

Commit 2147bb8

Browse files
author
Jason
committed
Add exception information to output when present.
1 parent 09c33ef commit 2147bb8

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/pythonjsonlogger/jsonlogger.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ def format(self, record):
121121
if "asctime" in self._required_fields:
122122
record.asctime = self.formatTime(record, self.datefmt)
123123

124+
# Display formatted exception, but allow overriding it in the
125+
# user-supplied dict.
126+
if record.exc_info and not message_dict.get('exc_info'):
127+
message_dict['exc_info'] = self.formatException(record.exc_info)
128+
124129
try:
125130
log_record = OrderedDict()
126131
except NameError:

tests/tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import json
44
import sys
5+
import traceback
56

67
try:
78
import xmlrunner
@@ -153,6 +154,22 @@ def process_log_record(self, log_record):
153154
logJson = json.loads(self.buffer.getvalue())
154155
self.assertEqual(logJson.get("custom"), "value")
155156

157+
def testExcInfo(self):
158+
fr = jsonlogger.JsonFormatter()
159+
self.logHandler.setFormatter(fr)
160+
try:
161+
raise Exception('test')
162+
except Exception:
163+
164+
self.logger.exception("hello")
165+
166+
expected_value = traceback.format_exc()
167+
# Formatter removes trailing new line
168+
if expected_value.endswith('\n'):
169+
expected_value = expected_value[:-1]
170+
171+
logJson = json.loads(self.buffer.getvalue())
172+
self.assertEqual(logJson.get("exc_info"), expected_value)
156173

157174
if __name__ == '__main__':
158175
if len(sys.argv[1:]) > 0:

0 commit comments

Comments
 (0)