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

Inconsistency in LogstashFormatterBase.serialize method for python 3

Open magrynow opened this issue 9 years ago • 1 comments

For python 3 LogstashFormatterBase.serialize method returns bytes instead of string. This incosistency results in no possibility in reusing this formatter with buildin handlers like pythons StreamHandlers (e.g. FileHandler), because it expects string object returned from formatter.

For python 2.X LogstashFormatterBase.serialize method behaves correct returning string.

magrynow avatar Apr 20 '15 11:04 magrynow

Same issue here. Python 3.5, when I use LogstashFormatter with FileHandler I get:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/logging/__init__.py", line 982, in emit
    stream.write(msg)
TypeError: write() argument must be str, not bytes

The issue is fixed once I override serialize to return json.dumps(message). What maintainers think about removing version check here and using json.dumps for all the versions? It makes sense to convert formatted messages to bytes in handlers before sending to a socket.

chekunkov avatar Aug 05 '16 13:08 chekunkov