Skip to content

Commit a525655

Browse files
logging
1 parent 16b6a13 commit a525655

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

backend/api/middlewares/logger_middleware.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from typing import Any
33
from fastapi import Request, Response
44
from starlette.middleware.base import BaseHTTPMiddleware
5+
from datetime import datetime
56

7+
from backend.utils.logging import logger
68

79
class LoggerMiddleware(BaseHTTPMiddleware):
810
async def dispatch(
@@ -14,9 +16,30 @@ async def dispatch(
1416
Logs should be printed so that they are easily readable and understandable.
1517
1618
:param request: Request received to this middleware from client (it is supplied by FastAPI)
17-
:param call_next: Endpoint or next middlew are to be called (if any, this is the next middleware in the chain of middlewares, it is supplied by FastAPI)
19+
:param call_next: Endpoint or next middle are to be called (if any, this is the next middleware in the chain of middlewares, it is supplied by FastAPI)
1820
:return: Response from endpoint
1921
"""
2022
# TODO:(Member) Finish implementing this method
23+
time_start = datetime.now()
24+
try:
25+
body = await request.json()
26+
except Exception:
27+
body = None
28+
29+
if body:
30+
params = body["params"]
31+
else:
32+
params = None
33+
34+
logger.info(f"Request params: {params}")
35+
logger.info(f"Time of call: {time_start}")
36+
2137
response = await call_next(request)
38+
39+
time_end = datetime.now()
40+
time_diff = time_end - time_start
41+
time_diff_ms = time_diff.total_seconds() * 1000
42+
43+
logger.info(f"Response Status: {response.status_code}")
44+
logger.info(f"Length of call (ms): {time_diff_ms}")
2245
return response

backend/data/data_models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def validate_params_format(self):
4040
return self
4141
else:
4242
raise ValueError()
43-
return self
4443

4544

4645
class Command(BaseSQLModel, table=True):

0 commit comments

Comments
 (0)