添加日志

This commit is contained in:
ChengCan
2026-03-26 11:25:34 +08:00
parent 3e3f4390a6
commit cff7ddf220
9 changed files with 351 additions and 4 deletions

View File

@@ -1,11 +1,14 @@
import os
import logging
from fastapi import FastAPI
try:
# For module mode: `uvicorn app.main:app`
from app.db import check_db_connection, close_db_engine
from app.logging_setup import configure_logging
from app.middleware.auth import install_auth_middleware
from app.middleware.request_log import install_request_logging_middleware
from app.routers.auth import router as auth_router
from app.routers.health import router as health_router
from app.routers.messages import router as messages_router
@@ -13,13 +16,19 @@ try:
except ModuleNotFoundError:
# For script mode: `python app/main.py` or VS Code "Run Python File"
from db import check_db_connection, close_db_engine
from logging_setup import configure_logging
from middleware.auth import install_auth_middleware
from middleware.request_log import install_request_logging_middleware
from routers.auth import router as auth_router
from routers.health import router as health_router
from routers.messages import router as messages_router
from settings import settings
configure_logging()
logger = logging.getLogger("app.main")
def create_app() -> FastAPI:
app = FastAPI(
title=settings.app_name,
@@ -30,12 +39,15 @@ def create_app() -> FastAPI:
@app.on_event("startup")
def on_startup() -> None:
check_db_connection()
logger.info("application started", extra={"event": "app_start"})
@app.on_event("shutdown")
def on_shutdown() -> None:
logger.info("application shutting down", extra={"event": "app_shutdown"})
close_db_engine()
install_auth_middleware(app)
install_request_logging_middleware(app)
app.include_router(auth_router)
app.include_router(health_router)
app.include_router(messages_router)