From a464297511a6e5f9d161b48202a15b26ab38376b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Sun, 19 Nov 2023 13:24:29 +0900 Subject: [PATCH] add api.log&errorlog->db --- backend/app/core/apiexception.py | 17 +++++++++++++++++ backend/app/db/crud.py | 10 +++++++++- backend/app/db/models.py | 10 +++++++++- backend/app/db/schemas.py | 7 ++++++- backend/app/main.py | 20 ++++++++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 backend/app/core/apiexception.py diff --git a/backend/app/core/apiexception.py b/backend/app/core/apiexception.py new file mode 100644 index 0000000..159110e --- /dev/null +++ b/backend/app/core/apiexception.py @@ -0,0 +1,17 @@ +from app.db.schemas import ErrorCreate +from app.db.session import SessionLocal +from app.db.crud import create_log + +class APIException(Exception): + + def __init__(self,location:str,title:str,content:str): + if(len(content) > 5000): + content =content[0:5000] + self.error = ErrorCreate(location=location,title=title,content=content) + +def writedblog(exc: APIException): + db = SessionLocal() + try: + create_log(db,exc.error) + finally: + db.close() \ No newline at end of file diff --git a/backend/app/db/crud.py b/backend/app/db/crud.py index d6a2860..1ec15e3 100644 --- a/backend/app/db/crud.py +++ b/backend/app/db/crud.py @@ -282,4 +282,12 @@ def get_eventactions(db: Session,eventid: str): eveactions = db.query(models.Action).join(models.EventAction,models.EventAction.actionid == models.Action.id ).join(models.Event,models.Event.id == models.EventAction.eventid).filter(models.Event.eventid == eventid).all() if not eveactions: raise HTTPException(status_code=404, detail="Data not found") - return eveactions \ No newline at end of file + return eveactions + + +def create_log(db: Session, error:schemas.ErrorCreate): + db_log = models.ErrorLog(location=error.location,title=error.title,content=error.content) + db.add(db_log) + db.commit() + db.refresh(db_log) + return db_log \ No newline at end of file diff --git a/backend/app/db/models.py b/backend/app/db/models.py index e49ef53..c2c5a0c 100644 --- a/backend/app/db/models.py +++ b/backend/app/db/models.py @@ -90,4 +90,12 @@ class EventAction(Base): __tablename__ = "eventaction" eventid = Column(Integer,ForeignKey("event.id")) - actionid = Column(Integer,ForeignKey("action.id")) \ No newline at end of file + actionid = Column(Integer,ForeignKey("action.id")) + + +class ErrorLog(Base): + __tablename__ = "errorlog" + id = Column(Integer, primary_key=True, index=True) + location = Column(String(200)) + title = Column(String(50)) + content = Column(String(5000)) \ No newline at end of file diff --git a/backend/app/db/schemas.py b/backend/app/db/schemas.py index a85e5ed..1effb2a 100644 --- a/backend/app/db/schemas.py +++ b/backend/app/db/schemas.py @@ -139,4 +139,9 @@ class Event(Base): mobile: bool class Config: - orm_mode = True \ No newline at end of file + orm_mode = True + +class ErrorCreate(BaseModel): + location:str + title:str + content:str \ No newline at end of file diff --git a/backend/app/main.py b/backend/app/main.py index 0b00200..79544db 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -11,6 +11,11 @@ from app.core.auth import get_current_active_user from app.core.celery_app import celery_app from app import tasks from fastapi.middleware.cors import CORSMiddleware +import logging +from app.core.apiexception import APIException, writedblog +from app.db.crud import create_log +from fastapi.responses import JSONResponse +import asyncio Base.metadata.create_all(bind=engine) @@ -37,6 +42,21 @@ app.add_middleware( # request.state.db.close() # return response +@app.on_event("startup") +async def startup_event(): + logger = logging.getLogger("uvicorn.access") + handler = logging.handlers.RotatingFileHandler("Log/api.log",mode="a",maxBytes = 100*1024, backupCount = 3) + handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) + logger.addHandler(handler) + +@app.exception_handler(APIException) +async def api_exception_handler(request: Request, exc: APIException): + loop = asyncio.get_event_loop() + loop.run_in_executor(None,writedblog,exc) + return JSONResponse( + status_code=400, + content={"detail": f"{exc.error.content}"}, + ) @app.get("/api/v1") async def root():