Skip to content

Instantly share code, notes, and snippets.

@cmandesign
Last active November 11, 2019 19:41
Show Gist options
  • Save cmandesign/f08c3d1bc583dff9c6cd2735ef81e37e to your computer and use it in GitHub Desktop.
Save cmandesign/f08c3d1bc583dff9c6cd2735ef81e37e to your computer and use it in GitHub Desktop.
Flask Log Request/Responses
@app.after_request
def after_request(response):
""" Logging all of the requests in JSON Per Line Format. """
inbound_request_logger = logging.getLogger('inbound_requests')
inbound_requests_logger.info({
"datetime": datetime.datetime.now().isoformat(),
"client_ip": request.remote_addr,
"method": request.method,
"request_url": request.path,
"response_status": response.status,
"request_referrer": request.referrer,
"request_user_agent": request.referrer,
"request_body": request.json,
"response_body": response.json
})
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment