Skip to content

Commit

Permalink
Add support for exc_traceback
Browse files Browse the repository at this point in the history
  • Loading branch information
EvaSDK committed May 27, 2015
1 parent 70d68cb commit 2cfa68c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions fluent/handler.py
Expand Up @@ -3,6 +3,7 @@
import logging
import socket
import sys
import traceback

try:
import simplejson as json
Expand All @@ -22,6 +23,10 @@ class FluentRecordFormatter(logging.Formatter, object):
Best used with server storing data in an ElasticSearch cluster for example.
Supports an extra `exc_traceback` format argument that represents a
traceback when logging an exception as return by
:meth:`traceback.extract_tb`.
:param fmt: a dict with format string as values to map to provided keys.
"""
def __init__(self, fmt=None, datefmt=None):
Expand All @@ -47,9 +52,17 @@ def format(self, record):
super(FluentRecordFormatter, self).format(record)
# Add ours
record.hostname = self.hostname

# Apply format
data = dict([(key, value % record.__dict__)
for key, value in self._fmt_dict.items()])
data = {}
for key, value in self._fmt_dict.items():
if value.find('%(exc_traceback)') >= 0:
if record.exc_info:
data[key] = traceback.extract_tb(record.exc_info[2])
else:
data[key] = None
else:
data[key] = value % record.__dict__

self._structuring(data, record.msg)
return data
Expand Down

0 comments on commit 2cfa68c

Please sign in to comment.