Skip to content

Commit 9cf49b1

Browse files
committed
Add support for customizing structured message
Should solve concerns from issue #7.
1 parent 098cd0a commit 9cf49b1

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

fluent/handler.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,35 @@
1616
from fluent import sender
1717

1818

19-
class FluentRecordFormatter(object):
20-
def __init__(self):
19+
class FluentRecordFormatter(logging.Formatter, object):
20+
""" A structured formatter for Fluent.
21+
22+
Best used with server storing data in an ElasticSearch cluster for example.
23+
24+
:param fmt: a dict with format string as values to map to provided keys.
25+
"""
26+
def __init__(self, fmt=None, datefmt=None):
27+
super(FluentRecordFormatter, self).__init__(None, datefmt)
28+
29+
if not fmt:
30+
self._fmt_dict = {
31+
'sys_host': '%(hostname)s',
32+
'sys_name': '%(name)s',
33+
'sys_module': '%(module)s',
34+
}
35+
else:
36+
self._fmt_dict = fmt
37+
2138
self.hostname = socket.gethostname()
2239

2340
def format(self, record):
24-
data = {'sys_host': self.hostname,
25-
'sys_name': record.name,
26-
'sys_module': record.module,
27-
# 'sys_lineno': record.lineno,
28-
# 'sys_levelno': record.levelno,
29-
# 'sys_levelname': record.levelname,
30-
# 'sys_filename': record.filename,
31-
# 'sys_funcname': record.funcName,
32-
# 'sys_exc_info': record.exc_info,
33-
}
34-
# if 'sys_exc_info' in data and data['sys_exc_info']:
35-
# data['sys_exc_info'] = self.formatException(data['sys_exc_info'])
41+
# Compute attributes handled by parent class.
42+
super(FluentRecordFormatter, self).format(record)
43+
# Add ours
44+
record.hostname = self.hostname
45+
# Apply format
46+
data = dict([(key, value % record.__dict__)
47+
for key, value in self._fmt_dict.items()])
3648

3749
self._structuring(data, record.msg)
3850
return data

0 commit comments

Comments
 (0)