elastic/ecs-logging-python

structlog integration - TypeError when passing object

scibi opened this issue · 0 comments

scibi commented

Right now when you add an object to your log like this:

            logger.info(
                "request_started",
                request=request,
                user_agent=request.META.get("HTTP_USER_AGENT"),
            )

You end up with TypeError like this: <WSGIRequest: GET '/healthcheck/'> is not JSON serializable in json_dumps (ecs_logging/_utils.py):

        if value:
            return "{%s,%s" % (
                ordered_json,
                json.dumps(value, sort_keys=True, separators=(",", ":"))[1:],
            )

You should use a default argument for json.dumps and provide fallback mechanism to handle this situation. See https://www.structlog.org/en/stable/_modules/structlog/processors.html#JSONRenderer and _json_fallback_handler.