mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-13 07:11:12 +08:00
将日志系统从 app/logger/ 移至 app/log/ 目录 将路由配置从 routers.py 重命名为 routes.py 将硬编码配置值移至 constants.py 中的默认常量 统一代码格式和导入排序 优化函数参数对齐方式
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import json
|
|
|
|
from fastapi import Request
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
from app.log.logger import get_request_logger
|
|
|
|
logger = get_request_logger()
|
|
|
|
|
|
# 添加中间件类
|
|
class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next):
|
|
# 记录请求路径
|
|
logger.info(f"Request path: {request.url.path}")
|
|
|
|
# 获取并记录请求体
|
|
try:
|
|
body = await request.body()
|
|
if body:
|
|
body_str = body.decode()
|
|
# 尝试格式化JSON
|
|
try:
|
|
formatted_body = json.loads(body_str)
|
|
logger.info(
|
|
f"Formatted request body:\n{json.dumps(formatted_body, indent=2, ensure_ascii=False)}"
|
|
)
|
|
except json.JSONDecodeError:
|
|
logger.info("Request body is not valid JSON.")
|
|
except Exception as e:
|
|
logger.error(f"Error reading request body: {str(e)}")
|
|
|
|
# 重置请求的接收器,以便后续处理器可以继续读取请求体
|
|
async def receive():
|
|
return {"type": "http.request", "body": body, "more_body": False}
|
|
|
|
request._receive = receive
|
|
|
|
response = await call_next(request)
|
|
return response
|