bobbui/json-logging-python

Add support for logging the response body

vstrimaitis opened this issue · 5 comments

I'm using Flask and would like to extend the current request logs to include the request and response bodies. This is what I've attempted so far:

from flask import request

class RequestFormatter(json_logging.JSONLogWebFormatter):
    def _format_log_object(
        self, record: logging.LogRecord, request_util: json_logging.util.RequestUtil
    ):
        log_obj = super()._format_log_object(record, request_util)
        log_obj.update({
            "request_body": request.get_data(as_text=True),
            "response_body": "???"
        })
        return log_obj

# ...

json_logging.init_request_instrument(app=app, custom_formatter=RequestFormatter)

I can't find a way of getting the response body without some dirty hacks (e.g. monkey patching FlaskAppRequestInstrumentationConfigurator). Am I missing something or is this feature not implemented? Thanks!

@vstrimaitis took a quick crunch, let me know if this working for you. Released 1.4.0rc for this. see doc here for more information: https://github.com/bobbui/json-logging-python#26-custom-log-formatter

After taking a quick look at the change it seems like this should indeed help, thanks! From the examples it's not entirely clear how I'd be able to access that request_info object in a custom formatter, but I can look into that in more detail on my own.

@vstrimaitis This is a simpler way to get the response object and do whatever u want with it
https://github.com/bobbui/json-logging-python/blob/1.4.0rc1/example/custom_log_format_request.py#L17

@vstrimaitis This is a simpler way to get the response object and do whatever u want with it
https://github.com/bobbui/json-logging-python/blob/1.4.0rc1/example/custom_log_format_request.py#L17

Looks great, thanks for the help!

qugu commented

Just a heads up for someone reading, here's a solution for getting the request/response body itself —

class CustomRequestJSONLog(json_logging.JSONRequestLogFormatter):
    """
        Customized logger
    """

    def _format_log_object(self, record, request_util):
        request = record.request_response_data._request
        response = record.request_response_data._response

        json_log_object = super(
            CustomRequestJSONLog, self)._format_log_object(
            record, request_util)
        json_log_object.update({
            "request_body": request.data.decode(),
            "response_body": response.data.decode()
        })
        return json_log_object