Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for logging the response body #68

Closed
vstrimaitis opened this issue Apr 9, 2021 · 5 comments
Closed

Add support for logging the response body #68

vstrimaitis opened this issue Apr 9, 2021 · 5 comments

Comments

@vstrimaitis
Copy link

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!

@bobbui
Copy link
Owner

bobbui commented Apr 9, 2021

@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

@bobbui bobbui closed this as completed Apr 9, 2021
@vstrimaitis
Copy link
Author

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.

@bobbui
Copy link
Owner

bobbui commented Apr 10, 2021

@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
Copy link
Author

@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
Copy link

qugu commented Dec 7, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants