python-logstash icon indicating copy to clipboard operation
python-logstash copied to clipboard

Use str() when json.dumps() finds an unserializable record

Open matt-snider opened this issue 8 years ago • 3 comments

Hi @vklochan

I ran into a logging error today involving a non-json serializable value in a situation similar to the following:

test_logger.info('testing', extra={'test_dict': {'foo': object()}})

Which would result in:

TypeError: <object object at 0x7f768bcef130> is not JSON serializable

Some of this is prevented in formatter.py#L45 where repr() is called on any non "easy" types. This is essentially what adding the default argument to json.dumps() would do, with the addition that it would also take care of nested structures. I can of course update my PR to use repr() instead of str().

matt-snider avatar Oct 17 '16 15:10 matt-snider

I hit this too. In my case the call to logging is from inside django. The code there is outside my control, so I can't work around this easily.

@vklochan could you consider merging this and publishing a new version on pypi?

@matt-snider thanks for sharing the fix with the world - I only found it after writing my own, inferior solution to the same problem

mozz100 avatar May 26 '17 12:05 mozz100

@mozz100 Glad it could be of some use to someone else :smiley:.Hoping this can be merged at some point soon as well but it's been quite awhile

matt-snider avatar May 28 '17 16:05 matt-snider

To me it feels like this project isn't being maintained any more (see https://github.com/vklochan/python-logstash/graphs/code-frequency)

Recommend others might be interested in developments at https://github.com/eht16/python-logstash-async

mozz100 avatar Apr 30 '19 15:04 mozz100