优化入口文件日志输出方式,解决启动日志在终端不显示的问题

This commit is contained in:
jingrow 2025-10-30 12:39:42 +08:00
parent 3059c81cc9
commit b0b570b637

View File

@ -5,6 +5,7 @@
Jingrow 应用入口 Jingrow 应用入口
""" """
import logging
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@ -14,6 +15,8 @@ from jingrow.services.whitelist import router
from jingrow.services.scheduler import get_scheduler_status from jingrow.services.scheduler import get_scheduler_status
from jingrow.core.hooks.init_hooks import init_hooks from jingrow.core.hooks.init_hooks import init_hooks
logger = logging.getLogger("uvicorn.error")
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""应用生命周期管理""" """应用生命周期管理"""
@ -21,29 +24,27 @@ async def lifespan(app: FastAPI):
try: try:
# 初始化钩子系统 # 初始化钩子系统
init_hooks() init_hooks()
print("钩子系统已初始化") logger.info("钩子系统已初始化")
# 启动调度器 # 启动调度器
await start_scheduler() await start_scheduler()
print("调度器已启动") logger.info("调度器已启动")
# 验证调度器状态 # 验证调度器状态
status = get_scheduler_status() status = get_scheduler_status()
print(f"调度器状态: 运行={status['running']}, 任务数={status['total_jobs']}") logger.info(f"调度器状态: 运行={status['running']}, 任务数={status['total_jobs']}")
except Exception as e: except Exception as e:
print(f"启动调度器失败: {e}") logger.exception(f"启动调度器失败: {e}")
import traceback
traceback.print_exc()
yield yield
# 关闭事件 # 关闭事件
try: try:
await stop_scheduler() await stop_scheduler()
print("调度器已停止") logger.info("调度器已停止")
except Exception as e: except Exception as e:
print(f"停止调度器失败: {e}") logger.exception(f"停止调度器失败: {e}")
def create_app(): def create_app():
"""创建FastAPI应用""" """创建FastAPI应用"""