Skip to content

Add support for logging the response body #68

Closed
@vstrimaitis

Description

@vstrimaitis

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!

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