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 Feb 3, 2015
1 parent fbd05d8 commit dd0cf23
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions fluent/handler.py
Expand Up @@ -2,6 +2,7 @@

import logging
import socket
import traceback

try:
import simplejson as json
Expand All @@ -21,6 +22,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 @@ -42,9 +47,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 dd0cf23

Please sign in to comment.