Created
January 3, 2022 09:29
-
-
Save num8er/1d8b40cbaacd4fd8aa67a41f02e35de2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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