Skip to content

Include exception stack trace in 'message' #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fluent/handler.py
Original file line number Diff line number Diff line change
@@ -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 = {}
20 changes: 20 additions & 0 deletions tests/test_asynchandler.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
@@ -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)