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 X-Forward-For option to Access Log #2123 #2124

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class AccessLogger:
Format:
%% The percent sign
%a Remote IP-address (IP-address of proxy if using reverse proxy)
%f X-Foward-For or Remote IP-address
%t Time when the request was started to process
%P The process ID of the child that serviced the request
%r First line of request
Expand All @@ -322,6 +323,7 @@ class AccessLogger:
"""
LOG_FORMAT_MAP = {
'a': 'remote_address',
'f': 'x_forwarded_for',
't': 'request_time',
'P': 'process_id',
'r': 'first_request_line',
Expand All @@ -336,7 +338,7 @@ class AccessLogger:
}

LOG_FORMAT = '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i"'
FORMAT_RE = re.compile(r'%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbOD]|Tf?)')
FORMAT_RE = re.compile(r'%(\{([A-Za-z0-9\-_]+)\}([ioe])|[aftPrsbOD]|Tf?)')
CLEANUP_RE = re.compile(r'(%[^s])')
_FORMAT_CACHE = {}

Expand Down Expand Up @@ -429,6 +431,13 @@ def _format_a(args):
else:
return peername

@staticmethod
def _format_f(args):
result = args[0].headers.get("X-Forwarded-For")
if result is None:
return AccessLogger._format_a(args)
return result

@staticmethod
def _format_t(args):
return datetime.datetime.utcnow().strftime('[%d/%b/%Y:%H:%M:%S +0000]')
Expand Down