Skip to content

Instantly share code, notes, and snippets.

@num8er
Created January 3, 2022 09:29
Show Gist options
  • Save num8er/1d8b40cbaacd4fd8aa67a41f02e35de2 to your computer and use it in GitHub Desktop.
Save num8er/1d8b40cbaacd4fd8aa67a41f02e35de2 to your computer and use it in GitHub Desktop.
from flask import request
from flask.logging import default_handler
class RequestFormatter(logging.Formatter):
def format(self, record):
record.url = request.url
record.remote_addr = request.remote_addr
if 'X-Forwarded-For' in request.headers:
proxy_data = request.headers['X-Forwarded-For']
ip_list = proxy_data.split(',')
record.remote_addr = ip_list[0]
return super(RequestFormatter, self).format(record)
formatter = RequestFormatter(
'[%(asctime)s] %(remote_addr)s requested %(url)s\n'
'%(levelname)s in %(module)s: %(message)s'
)
default_handler.setFormatter(formatter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment