Skip to content

Commit 84f5fa9

Browse files
authored
update access-log (#9)
1 parent bd8c028 commit 84f5fa9

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

backend/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ RUN pip install --no-cache-dir -U -r backend/requirements.txt
1010
ENV PYTHONPATH=/app
1111
EXPOSE 8080
1212

13-
ENTRYPOINT ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "4"]
13+
ENTRYPOINT ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "4", "--no-access-log"]

backend/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ async def lifespan(app: FastAPI):
5757
if __name__ == "__main__":
5858
import uvicorn
5959

60-
uvicorn.run(app, host="0.0.0.0", port=8080)
60+
uvicorn.run(app, host="0.0.0.0", port=8080, access_log=False)

backend/middleware/logging.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import time
34
from fastapi import Request
@@ -9,6 +10,14 @@
910
class AccessLogMiddleware(BaseHTTPMiddleware):
1011
async def dispatch(self, request: Request, call_next):
1112
start = time.time()
13+
model = "-"
14+
if request.method in ("POST", "PUT", "PATCH"):
15+
try:
16+
body = await request.body()
17+
data = json.loads(body)
18+
model = data.get("model", "-")
19+
except Exception:
20+
pass
1221
response = await call_next(request)
1322
duration_ms = (time.time() - start) * 1000
1423
token = request.headers.get("authorization", "")
@@ -17,10 +26,11 @@ async def dispatch(self, request: Request, call_next):
1726
else:
1827
token = "-"
1928
access_logger.info(
20-
"%s %s %s %d %.0fms",
29+
"%s %s %s model=%s %d %.0fms",
2130
token,
2231
request.method,
2332
request.url.path,
33+
model,
2434
response.status_code,
2535
duration_ms,
2636
)

0 commit comments

Comments
 (0)