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

convert 'None' and '' string values to NoneType before forwarding the… #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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