Skip to content

Nested json serialization problem #60

@sallyruthstruik

Description

@sallyruthstruik

Got an error with logging inner json:

test_logger.info('python-logstash: test extra fields', extra=extra)

class Test:

    def __init__(self):
        self.a = 1
        self.b = 1

    def __repr__(self):
        return "Test(a={}, b={})".format(self.a, self.b)

test_logger.info("test inner json", extra={"meta": {"test": Test()}})

Gives:

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/handlers.py", line 577, in emit
    s = self.makePickle(record)
  File "/home/stas/workspace/partners/python-logstash/logstash/handler_udp.py", line 17, in makePickle
    return self.formatter.format(record)
  File "/home/stas/workspace/partners/python-logstash/logstash/formatter.py", line 142, in format
    return self.serialize(message)
  File "/home/stas/workspace/partners/python-logstash/logstash/formatter.py", line 83, in serialize
    return json.dumps(message)
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Test(a=1, b=1) is not JSON serializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions