tintoy / seqlog

Python logging integration for Seq (https://getseq.net)
https://seqlog.readthedocs.io/
MIT License
17 stars 11 forks source link

JSON serializable Error on Debug level #25

Closed lastboy closed 3 years ago

lastboy commented 4 years ago

Description

I have configured logging from a file using seqlog docs

What I Did

All is working as expected until I switched from LEVEL INFO to DEBUG. I got a JSON parse error: "TypeError: Object of type bytes is not JSON serializable", probably about some third-party log.

I modified the code at structured_logging.py file and caught the exception on the statement: ...json.dumps(request_body... and just returned an empty Events so the seqlog could continue w/o an exception.

Please suggest...

Thank you, -Arik

tintoy commented 4 years ago

Hi - sorry for the delay, I'll take a look at this in an hour or 2 :)

tintoy commented 4 years ago

Ok, should be fixed in v0.3.17.

lukeamcmurray commented 4 years ago

I faced the same issue with some 3rd party modules containing objects that could not be serialized. To get around this i added a custom JSONEncoder, which returns a string for these exceptions, and used it in my logging setup:

json_extensions.py

import json

class LoggingJSONEncoder(json.JSONEncoder):

    def default(self, obj):
        try:
            return json.JSONEncoder.default(self, obj)
        except:
            return str(obj)

app.py


import seqlog

seqlog.log_to_seq(
   server_url="http://my-seq-server:5341/",
   api_key="My API Key",
   level=logging.INFO,
   batch_size=10,
   auto_flush_timeout=10,
   override_root_logger=True,
   json_encoder_class=json_extensions.LoggingJSONEncoder
)
piotrmaslanka commented 3 years ago

duplicate of #13. Closing.