2
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-10 01:13:33 +00:00

Log Forwarded for address in accesslog instead of client address for tornado

This commit is contained in:
Ozzie Isaacs 2024-07-08 15:04:53 +02:00
parent 1d7dcce98d
commit de989783e8

View File

@ -22,6 +22,7 @@ import tornado
from tornado import escape
from tornado import httputil
from tornado.ioloop import IOLoop
from tornado.log import access_log
from typing import List, Tuple, Optional, Callable, Any, Dict, Text
from types import TracebackType
@ -96,5 +97,26 @@ class MyWSGIContainer(WSGIContainer):
except TypeError as e:
environ = WSGIContainer.environ(request)
environ['RAW_URI'] = request.path
self.env = environ
return environ
def _log(self, status_code: int, request: httputil.HTTPServerRequest) -> None:
if status_code < 400:
log_method = access_log.info
elif status_code < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * request.request_time()
assert request.method is not None
assert request.uri is not None
ip = self.env.get("HTTP_FORWARD_FOR", None) or request.remote_ip
summary = (
request.method # type: ignore[operator]
+ " "
+ request.uri
+ " ("
+ ip
+ ")"
)
log_method("%d %s %.2fms", status_code, summary, request_time)