Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39e60c0

Browse filesBrowse files
committed
Include the stack trace in 'message' during exceptions.
Brings fluent logging more inline with normal log formatters. Utilize logging.Formatter.format() which combines record.msg with the exc_text. Signed-off-by: John Paulett <john@paulett.org>
1 parent 268f577 commit 39e60c0
Copy full SHA for 39e60c0

File tree

Expand file treeCollapse file tree

3 files changed

+43
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+43
-2
lines changed

‎fluent/handler.py

Copy file name to clipboardExpand all lines: fluent/handler.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ def _format_msg_json(self, record, msg):
131131
if isinstance(json_msg, dict):
132132
return json_msg
133133
else:
134-
return {'message': str(json_msg)}
134+
return self._format_msg_default(record, msg)
135135
except ValueError:
136136
return self._format_msg_default(record, msg)
137137

138138
def _format_msg_default(self, record, msg):
139-
return {'message': record.getMessage()}
139+
return {'message': super(FluentRecordFormatter, self).format(record)}
140140

141141
def _format_by_exclusion(self, record):
142142
data = {}

‎tests/test_asynchandler.py

Copy file name to clipboardExpand all lines: tests/test_asynchandler.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@ def test_non_string_dict_message(self):
239239
# For some reason, non-string keys are ignored
240240
self.assertFalse(42 in data[0][2])
241241

242+
def test_exception_message(self):
243+
handler = self.get_handler_class()('app.follow', port=self._port)
244+
245+
with handler:
246+
logging.basicConfig(level=logging.INFO)
247+
log = logging.getLogger('fluent.test')
248+
handler.setFormatter(fluent.handler.FluentRecordFormatter())
249+
log.addHandler(handler)
250+
try:
251+
raise Exception('sample exception')
252+
except Exception:
253+
log.exception('it failed')
254+
255+
data = self.get_data()
256+
message = data[0][2]['message']
257+
# Includes the logged message, as well as the stack trace.
258+
self.assertTrue('it failed' in message)
259+
self.assertTrue('tests/test_asynchandler.py", line' in message)
260+
self.assertTrue('Exception: sample exception' in message)
261+
242262

243263
class TestHandlerWithCircularQueue(unittest.TestCase):
244264
Q_SIZE = 3

‎tests/test_handler.py

Copy file name to clipboardExpand all lines: tests/test_handler.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,24 @@ def test_non_string_dict_message(self):
350350
data = self.get_data()
351351
# For some reason, non-string keys are ignored
352352
self.assertFalse(42 in data[0][2])
353+
354+
def test_exception_message(self):
355+
handler = fluent.handler.FluentHandler('app.follow', port=self._port)
356+
357+
with handler:
358+
logging.basicConfig(level=logging.INFO)
359+
log = logging.getLogger('fluent.test')
360+
handler.setFormatter(fluent.handler.FluentRecordFormatter())
361+
log.addHandler(handler)
362+
try:
363+
raise Exception('sample exception')
364+
except Exception:
365+
log.exception('it failed')
366+
log.removeHandler(handler)
367+
368+
data = self.get_data()
369+
message = data[0][2]['message']
370+
# Includes the logged message, as well as the stack trace.
371+
self.assertTrue('it failed' in message)
372+
self.assertTrue('tests/test_handler.py", line' in message)
373+
self.assertTrue('Exception: sample exception' in message)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.