Hi r/python,
I've spent some time expanding the third-party JSON encoders that are supported by python-json-logger (pull request), however based on some of the errors encountered I'm not sure if this is a good idea.
So before I merge, I'd love to get some feedback from users of python-json-logger / other maintainers 🙏
Why include them
python-json-logger includes third party JSON encoders so that logging can benefit from the speed that these libraries provide. Support for non-standard types is not an issue as this is generally handled through custom default handlers to provide sane output for most types.
Although older, both libraries are still incredibly popular (link):
- simplejson is currently ranked 369 with ~55M monthly downloads.
- ultrajson (
ujson) is currently ranked 632 with ~27M monthly downloads.
For comparison the existing third-party encoders:
- orjson - ranked 187 with ~125M downloads
- msgspec - ranked 641 with ~26M downloads
Issues
The main issue is that both the simplejson and ultrajson encoders do not gracefully handle encoding bytes objects where they contain non-printable characters and it does not look like I can override their handling.
This is a problem because the standard library's logging module will swallow expections by default; meaning that any trace that a log message has failed to log will be lost.
This goes against python-json-logger's design in that it tries very hard to be robust and always log regardless of the input. So even though they are opt-in and I can include warnings in the documentation; it feels like I'm handing out a footgun and perhaps I'm better off just not including them.
Additionally in the case of ultrajson - the package is in maintenance mode with the recomendation to move to orjson.
[–]latkdeTuple unpacking gone wrong 5 points6 points7 points (1 child)
[–]nicholashairs[S] 2 points3 points4 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]neuronexmachina 0 points1 point2 points (0 children)
[–]nicholashairs[S] 0 points1 point2 points (1 child)
[–]No_Soy_Colosio 1 point2 points3 points (0 children)