added aiohttp logger

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-03-10 01:05:37 +01:00
Ursprung 568d0e295a
Commit a615906dcb
2 geänderte Dateien mit 35 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -24,6 +24,8 @@ classifiers = [
] ]
dependencies = ["gunicorn", "systemd_python"] dependencies = ["gunicorn", "systemd_python"]
[project.optional-dependencies]
aiohttp = ["aiohttp"]
[project.urls] [project.urls]
Documentation = "https://github.com/unknown/gunicorn-logging-extension#readme" Documentation = "https://github.com/unknown/gunicorn-logging-extension#readme"
Issues = "https://github.com/unknown/gunicorn-logging-extension/issues" Issues = "https://github.com/unknown/gunicorn-logging-extension/issues"
@ -34,6 +36,7 @@ path = "src/gunicorn_logging_extension/__init__.py"
[tool.hatch.build] [tool.hatch.build]
directory = "../dist" directory = "../dist"
[tool.hatch.envs.default] [tool.hatch.envs.default]
dependencies = ["coverage[toml]>=6.5", "pytest"] dependencies = ["coverage[toml]>=6.5", "pytest"]
[tool.hatch.envs.default.scripts] [tool.hatch.envs.default.scripts]

Datei anzeigen

@ -0,0 +1,32 @@
from aiohttp.abc import AbstractAccessLogger
from aiohttp.web_request import BaseRequest
from aiohttp.web_response import StreamResponse
from . import REDIRECT_CODES
class AccessLogger(AbstractAccessLogger):
def log(self, request: BaseRequest, response: StreamResponse, time: float):
level = self.logger.info
if response.status >= 400:
level = self.logger.warning
elif response.status >= 500:
level = self.logger.error
extra = dict(
METHOD=request.method,
PATH=request.rel_url,
QUERY=request.query_string,
PROTOCOL=request.scheme,
TIME=time,
STATUS_CODE=response.status_code,
REMOTE=request.remote,
LENGTH=response.content_length,
)
if request.headers.get("Referer", False):
extra["REFERER"] = request.headers.get("Referer")
if request.headers.get("user-agent", False):
extra["USER_AGENT"] = request.headers.get("user-agent")
location=""
if response.status_code in REDIRECT_CODES:
extra["LOCATION"] = request.headers.get("location")
location = f" -> {extra["LOCATION"]}"
level(f"Access({response.status}) {request.method} {request.rel_url}{}", extra=extra)