python-logstash
python-logstash copied to clipboard
Use str() when json.dumps() finds an unserializable record
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()
.
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 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
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