Skip to content
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

Add formatted exception #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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