This commit is contained in:
2026-04-21 19:00:53 +05:45
commit ebb12e6ab7
20 changed files with 1391 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
import json
import uuid
from datetime import datetime, UTC
from starlette.responses import StreamingResponse
from .db.db import get_db
from .models import ApiLog
def write_log(req: Request, res: StreamingResponse, req_body : dict, res_body: str, process_time : float):
db = next(get_db())
try:
res_body = json.loads(res_body)
except Exception:
res_body = None
client_ip = req.client.host
if client_ip == 'testclient':
client_ip = '127.0.0.1'
log = ApiLog(
api_key = uuid.UUID(req.headers.get("x-api-key")) if req.headers.get("x-api-key") else None,
ip_address = client_ip,
path = req.url.path,
method = req.method,
status_code = res.status_code,
request_body = req_body,
response_body = res_body,
query_params = dict(req.query_params),
path_params = req.path_params,
process_time = process_time,
created_at = datetime.now(UTC)
)
db.add(log)
db.commit()
db.close()