Skip to content

Commit

Permalink
convert 'None' and '' string values to NoneType before forwarding the…
Browse files Browse the repository at this point in the history
…m to fluentd
  • Loading branch information
T0MASD authored and RH-Docs committed May 23, 2017
1 parent 1c69535 commit b290dbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fluent/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ def _structuring(self, data, record):
self._add_dic(data, {'message': msg})

@staticmethod
def _add_dic(data, dic):
def _add_dic(data, dic, nonevalues=['None','']):
for key, value in dic.items():
if isinstance(key, basestring):
data[str(key)] = value
data[str(key)] = None if value in nonevalues else value


class FluentHandler(logging.Handler):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ def test_custom_field_fill_missing_fmt_key_is_true(self):
# field defaults to none if not in log record
self.assertIsNone(data[0][2]['custom_field'])

def test_custom_field_convert_none_strings(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',
}
)
)
log.addHandler(handler)
log.info({'name': 'None', 'sample':''})
log.removeHandler(handler)
handler.close()

data = self.get_data()
# field should be none
self.assertIsNone(data[0][2]['name'])
self.assertIsNone(data[0][2]['sample'])


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

Expand Down

0 comments on commit b290dbd

Please sign in to comment.