Skip to content

Commit

Permalink
Merge pull request #131 from johnpaulett/include-stack-trace
Browse files Browse the repository at this point in the history
Include exception stack trace in 'message'
  • Loading branch information
arcivanov committed Apr 11, 2018
2 parents cf769b7 + 39e60c0 commit c107b95
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fluent/handler.py
Expand Up @@ -131,12 +131,12 @@ def _format_msg_json(self, record, msg):
if isinstance(json_msg, dict):
return json_msg
else:
return {'message': str(json_msg)}
return self._format_msg_default(record, msg)
except ValueError:
return self._format_msg_default(record, msg)

def _format_msg_default(self, record, msg):
return {'message': record.getMessage()}
return {'message': super(FluentRecordFormatter, self).format(record)}

def _format_by_exclusion(self, record):
data = {}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_asynchandler.py
Expand Up @@ -239,6 +239,26 @@ def test_non_string_dict_message(self):
# For some reason, non-string keys are ignored
self.assertFalse(42 in data[0][2])

def test_exception_message(self):
handler = self.get_handler_class()('app.follow', port=self._port)

with handler:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger('fluent.test')
handler.setFormatter(fluent.handler.FluentRecordFormatter())
log.addHandler(handler)
try:
raise Exception('sample exception')
except Exception:
log.exception('it failed')

data = self.get_data()
message = data[0][2]['message']
# Includes the logged message, as well as the stack trace.
self.assertTrue('it failed' in message)
self.assertTrue('tests/test_asynchandler.py", line' in message)
self.assertTrue('Exception: sample exception' in message)


class TestHandlerWithCircularQueue(unittest.TestCase):
Q_SIZE = 3
Expand Down
21 changes: 21 additions & 0 deletions tests/test_handler.py
Expand Up @@ -350,3 +350,24 @@ def test_non_string_dict_message(self):
data = self.get_data()
# For some reason, non-string keys are ignored
self.assertFalse(42 in data[0][2])

def test_exception_message(self):
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

with handler:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger('fluent.test')
handler.setFormatter(fluent.handler.FluentRecordFormatter())
log.addHandler(handler)
try:
raise Exception('sample exception')
except Exception:
log.exception('it failed')
log.removeHandler(handler)

data = self.get_data()
message = data[0][2]['message']
# Includes the logged message, as well as the stack trace.
self.assertTrue('it failed' in message)
self.assertTrue('tests/test_handler.py", line' in message)
self.assertTrue('Exception: sample exception' in message)

0 comments on commit c107b95

Please sign in to comment.