logging enhancements
This commit is contained in:
parent
2dec6d5384
commit
569aefeccb
8 changed files with 88 additions and 20 deletions
0
app/web/__init__.py
Normal file
0
app/web/__init__.py
Normal file
24
app/web/middlewares.py
Normal file
24
app/web/middlewares.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import logging
|
||||
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import Request
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
client = f"{request.client.host}:{request.client.port}"
|
||||
match request.method:
|
||||
case "POST" | "PUT" | "DELETE" if request.headers.get("Content-Type") == "application/json":
|
||||
body = await request.body()
|
||||
logger.trace(f"Request from {client}: {body.decode()}")
|
||||
case "GET":
|
||||
logger.trace(f"Request from {client}")
|
||||
case _:
|
||||
logger.trace(f"Request from {client} (content-type:{request.headers.get("Content-Type")})")
|
||||
|
||||
response = await call_next(request)
|
||||
logger.trace(f"Respone: {response.status_code} {type(response)}")
|
||||
|
||||
return response
|
||||
Loading…
Add table
Add a link
Reference in a new issue