8000 Introduce flexible record formatter by sleongkoan · Pull Request #73 · fluent/fluent-logger-python · GitHub
[go: up one dir, main page]

Skip to content

Introduce flexible record formatter #73

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions fluent/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ class FluentRecordFormatter(logging.Formatter, object):
Best used with server storing data in an ElasticSearch cluster for example.

:param fmt: a dict with format string as values to map to provided keys.
:param datefmt: strftime()-compatible date/time format string.
:param style: (NOT USED)
:param fill_missing_fmt_key: if True, do not raise a KeyError if the format
key is not found. Put None if not found.s
"""
def __init__(self, fmt=None, datefmt=None, style='%'):
def __init__(self, fmt=None, datefmt=None, style='%', fill_missing_fmt_key=False):
super(FluentRecordFormatter, self).__init__(None, datefmt)

if not fmt:
Expand All @@ -38,6 +42,8 @@ def __init__(self, fmt=None, datefmt=None, style='%'):

self.hostname = socket.gethostname()

self.fill_missing_fmt_key = fill_missing_fmt_key

def format(self, record):
# Only needed for python2.6
if sys.version_info[0:2] <= (2, 6) and self.usesTime():
Expand All @@ -47,9 +53,18 @@ 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():
try:
value = value % record.__dict__
except KeyError as exc:
value = None
if not self.fill_missing_fmt_key:
raise exc

data[key] = value

self._structuring(data, record)
return data
Expand Down
42 changes: 42 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ def test_custom_fmt(self):
self.assertTrue('lineno' in data[0][2])
self.assertTrue('emitted_at' in data[0][2])

def test_custom_field_raise_exception(self):
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

logging.basicConfig(level=logging.INFO)
log = logging.getLogger('fluent.test')
handler.setFormatter(
fluent.handler.FluentRecordFormatter(fmt={
'name': '%(name)s',
'custom_field': '%(custom_field)s'
})
)
log.addHandler(handler)
with self.assertRaises(KeyError):
log.info({'sample': 'value'})
log.removeHandler(handler)
handler.close()

def test_custom_field_fill_missing_fmt_key_is_true(self):
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

logging.basicConfig(level=logging.INFO)
log = logging.getLogger('fluent.test')
handler.setFormatter(
fluent.handler.FluentRecordFormatter(fmt={
'name': '%(name)s',
'custom_field': '%(custom_field)s'
},
fill_missing_fmt_key=True
)
)
log.addHandler(handler)
log.info({'sample': 'value'})
log.removeHandler(handler)
handler.close()

data = self.get_data()
self.assertTrue('name' in data[0][2])
self.assertEqual('fluent.test', data[0][2]['name'])
self.assertTrue('custom_field' in data[0][2])
# field defaults to none if not in log record
self.assertIsNone(data[0][2]['custom_field'])

def test_json_encoded_message(self):
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

Expand Down
0