update operation
This commit is contained in:
@@ -15,7 +15,6 @@ def get_db(request: Request,tenant:str = "1",tenantdb = Depends(get_tenant_db)):
|
||||
db.close()
|
||||
|
||||
|
||||
def get_log_db(request: Request):
|
||||
def get_log_db():
|
||||
db = tenantdb.get_db()
|
||||
request.state.db = db
|
||||
return db
|
||||
@@ -1,12 +1,12 @@
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.models import OperationLog,User
|
||||
|
||||
from functools import wraps
|
||||
from fastapi import Request
|
||||
from contextvars import ContextVar
|
||||
from app.core.apiexception import APIException
|
||||
from app.core.dbmanager import get_log_db
|
||||
from app.db.crud import create_log
|
||||
import json
|
||||
|
||||
class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
@@ -24,7 +24,10 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
state = request.state
|
||||
except Exception as e:
|
||||
await self.log_error(request, e)
|
||||
raise
|
||||
response = JSONResponse(
|
||||
content={"detail": "Internal Server Error"},
|
||||
status_code=500
|
||||
)
|
||||
|
||||
if hasattr(request.state, "user") and hasattr(request.state, "tenant"):
|
||||
await self.log_request(request, response,state)
|
||||
@@ -51,9 +54,13 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
print(f"Logging failed: {str(e)}")
|
||||
|
||||
|
||||
async def log_error(self, request: Request, exc: Exception):
|
||||
# 错误处理逻辑
|
||||
pass
|
||||
async def log_error(self, request: Request, e: Exception):
|
||||
exc = APIException('operation:dispatch',request.url._url,f"Error occurred while writting operation log:",e)
|
||||
db = get_log_db()
|
||||
try:
|
||||
create_log(db,exc.error)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def write_log_to_db(self, db_operation,db):
|
||||
db.add(db_operation)
|
||||
|
||||
Reference in New Issue
Block a user