Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request madzak#29 from landreville/master
Browse files Browse the repository at this point in the history
Add exception information to output when present.
  • Loading branch information
Zakaria Zajac committed Jun 12, 2015
2 parents 09c33ef + 2147bb8 commit 3c6e6d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def format(self, record):
if "asctime" in self._required_fields:
record.asctime = self.formatTime(record, self.datefmt)

# Display formatted exception, but allow overriding it in the
# user-supplied dict.
if record.exc_info and not message_dict.get('exc_info'):
message_dict['exc_info'] = self.formatException(record.exc_info)

try:
log_record = OrderedDict()
except NameError:
Expand Down
17 changes: 17 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import json
import sys
import traceback

try:
import xmlrunner
Expand Down Expand Up @@ -153,6 +154,22 @@ def process_log_record(self, log_record):
logJson = json.loads(self.buffer.getvalue())
self.assertEqual(logJson.get("custom"), "value")

def testExcInfo(self):
fr = jsonlogger.JsonFormatter()
self.logHandler.setFormatter(fr)
try:
raise Exception('test')
except Exception:

self.logger.exception("hello")

expected_value = traceback.format_exc()
# Formatter removes trailing new line
if expected_value.endswith('\n'):
expected_value = expected_value[:-1]

logJson = json.loads(self.buffer.getvalue())
self.assertEqual(logJson.get("exc_info"), expected_value)

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

0 comments on commit 3c6e6d6

Please sign in to comment.