|
|
@@ -1,4 +1,7 @@
|
|
|
-from fastapi import FastAPI
|
|
|
+import logging
|
|
|
+import time
|
|
|
+from datetime import datetime
|
|
|
+from fastapi import FastAPI, Request
|
|
|
from alien_store.api.router import router
|
|
|
from alien_gateway.config import settings
|
|
|
|
|
|
@@ -7,6 +10,27 @@ app = FastAPI(
|
|
|
version="1.0.0"
|
|
|
)
|
|
|
|
|
|
+logger = logging.getLogger("alien_store")
|
|
|
+
|
|
|
+# 轻量日志中间件:仅记录方法、路径、状态码、耗时和客户端
|
|
|
+@app.middleware("http")
|
|
|
+async def log_requests(request: Request, call_next):
|
|
|
+ start = time.perf_counter()
|
|
|
+ try:
|
|
|
+ response = await call_next(request)
|
|
|
+ return response
|
|
|
+ finally:
|
|
|
+ duration_ms = (time.perf_counter() - start) * 1000
|
|
|
+ logger.info(
|
|
|
+ "[%s] HTTP %s %s -> %s (%.2f ms) client=%s",
|
|
|
+ datetime.utcnow().isoformat(timespec="milliseconds") + "Z",
|
|
|
+ request.method,
|
|
|
+ request.url.path,
|
|
|
+ getattr(response, "status_code", "N/A"),
|
|
|
+ duration_ms,
|
|
|
+ request.client.host if request.client else "-",
|
|
|
+ )
|
|
|
+
|
|
|
# 挂载业务路由
|
|
|
app.include_router(router, prefix="/api/store", tags=["Store"])
|
|
|
|