feat: 日志模块

This commit is contained in:
李如威 2025-08-22 11:26:02 +08:00
parent ad05f55a04
commit 8cedbc8dca
6 changed files with 15 additions and 8 deletions

View File

@ -0,0 +1,7 @@
import logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s:\n%(message)s"
)
logger = logging.getLogger("workflow")

View File

@ -3,8 +3,9 @@ from fastapi import FastAPI
from models.schemas.system import InitStatusResponse, InitConfigRequest from models.schemas.system import InitStatusResponse, InitConfigRequest
from core.config import load_init_config, save_init_config from core.config import load_init_config, save_init_config
from core.db import init_tortoise, close_tortoise from core.db import init_tortoise, close_tortoise
from core.logger import logger
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import logging
async def is_init(): async def is_init():
init_config = await load_init_config() init_config = await load_init_config()
@ -16,17 +17,18 @@ async def is_init():
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
if await is_init(): if await is_init():
app.logger.info("初始化数据库") logger.info("初始化数据库")
await init_tortoise() init_config = await load_init_config()
await init_tortoise(init_config["db_type"], init_config)
yield yield
app.logger.info("关闭数据库") logger.info("关闭数据库")
await close_tortoise() await close_tortoise()
def create_app(): def create_app():
app = FastAPI(title="Workflow API", version="1.0.0") app = FastAPI(title="Workflow API", version="1.0.0", lifespan=lifespan)
@app.get("/") @app.get("/")
async def root(): async def root():
@ -48,7 +50,7 @@ def create_app():
response_model=InitStatusResponse, response_model=InitStatusResponse,
) )
async def init_system(request: InitConfigRequest): async def init_system(request: InitConfigRequest):
app.logger.info("init_system") logger.info("init_system")
try: try:
init_config = await load_init_config() init_config = await load_init_config()
if init_config.get('is_init'): if init_config.get('is_init'):
@ -75,8 +77,6 @@ def create_app():
app = create_app() app = create_app()
app.logger = logging.getLogger("workflow")
app.logger.setLevel(logging.INFO)
if __name__ == "__main__": if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True) uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)

BIN
backend/your_workflow Normal file

Binary file not shown.

BIN
backend/your_workflow-shm Normal file

Binary file not shown.