From b37f78b961e4939b64c9f02b994cd346a119e155 Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Mon, 2 Feb 2026 21:22:03 +0100 Subject: [PATCH] added logging middleware to log to stdout --- pillar_tool/main.py | 4 ++++ pillar_tool/middleware/basicauth_backend.py | 3 +-- pillar_tool/middleware/db_connection.py | 1 - pillar_tool/middleware/logging.py | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 pillar_tool/middleware/logging.py diff --git a/pillar_tool/main.py b/pillar_tool/main.py index 1cb817f..c596fbe 100644 --- a/pillar_tool/main.py +++ b/pillar_tool/main.py @@ -1,5 +1,8 @@ # load config so everything else can work +from http.server import BaseHTTPRequestHandler + from pillar_tool.db.base_model import as_dict +from pillar_tool.middleware.logging import request_logging_middleware from pillar_tool.schemas import HostCreateParams from pillar_tool.util import load_config, config load_config() @@ -59,6 +62,7 @@ def on_general_error(request: Request, exc: Exception): app = FastAPI() app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend(), on_error=on_auth_error) app.add_middleware(BaseHTTPMiddleware, dispatch=db_connection_middleware) +app.add_middleware(BaseHTTPMiddleware, dispatch=request_logging_middleware) app.exception_handler(Exception)(on_general_error) diff --git a/pillar_tool/middleware/basicauth_backend.py b/pillar_tool/middleware/basicauth_backend.py index 339cf34..c0bead4 100644 --- a/pillar_tool/middleware/basicauth_backend.py +++ b/pillar_tool/middleware/basicauth_backend.py @@ -32,8 +32,7 @@ class BasicAuthBackend(AuthenticationBackend): except (ValueError, UnicodeDecodeError, binascii.Error): raise AuthenticationError('Invalid basic auth credentials') - username, what, password = decoded.partition(":") - print(username, what, password) + username, _this_should_be_a_colon, password = decoded.partition(":") user = verify_user(conn.state.db, username, password) if user is None: diff --git a/pillar_tool/middleware/db_connection.py b/pillar_tool/middleware/db_connection.py index f471fa7..8610c08 100644 --- a/pillar_tool/middleware/db_connection.py +++ b/pillar_tool/middleware/db_connection.py @@ -7,7 +7,6 @@ from pillar_tool.db.database import get_connection async def db_connection_middleware(request: Request, call_next: Callable) -> Response: session = get_connection() - print("test 1") request.state.db = session try: diff --git a/pillar_tool/middleware/logging.py b/pillar_tool/middleware/logging.py new file mode 100644 index 0000000..40a2361 --- /dev/null +++ b/pillar_tool/middleware/logging.py @@ -0,0 +1,15 @@ +from datetime import datetime, timezone +from typing import Callable + +from starlette.requests import Request +from starlette.responses import Response + + +async def request_logging_middleware(request: Request, call_next: Callable) -> Response: + timestamp = datetime.now(timezone.utc).astimezone() + response: Response = await call_next(request) + print(f"{timestamp.isoformat()} {request.method} {request.url.path} {response.status_code} {response.headers.get("content-length")} \"{request.headers.get("user-agent") or '-'}\"") + + return response + +