mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-05 23:56:37 +08:00
引入可配置的日志级别功能,允许用户通过配置编辑器和 `.env` 文件设置所需的日志详细程度。
主要变化:
- 在 `.env.example` 和 `app/config/config.py` 中添加了 `LOG_LEVEL` 设置。
- 修改了 `app/log/logger.py`,使其从设置中读取日志级别,并实现了对现有 logger 进行动态日志级别更新的功能。
- 更新了 `app/router/config_routes.py`,以便在保存配置后触发日志级别更新。
- 在 `app/templates/config_editor.html` 和 `app/static/js/config_editor.js` 中添加了日志级别选择的 UI 元素。
- 将 `app/router/gemini_routes.py` 和 `app/router/openai_routes.py` 中的一些日志调用从 `info` 调整为 `debug`,以降低默认输出的详细程度。
- 在 `README.md` 的“特别鸣谢”部分添加了 🎉 表情符号。
54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
"""
|
|
配置路由模块
|
|
"""
|
|
from typing import Any, Dict
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
from app.core.security import verify_auth_token
|
|
from app.log.logger import get_config_routes_logger, Logger # 导入 Logger 类
|
|
from app.service.config.config_service import ConfigService
|
|
|
|
# 创建路由
|
|
router = APIRouter(prefix="/api/config", tags=["config"])
|
|
|
|
logger = get_config_routes_logger()
|
|
|
|
|
|
@router.get("", response_model=Dict[str, Any])
|
|
async def get_config(request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
return await ConfigService.get_config()
|
|
|
|
|
|
@router.put("", response_model=Dict[str, Any])
|
|
async def update_config(config_data: Dict[str, Any], request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
try:
|
|
result = await ConfigService.update_config(config_data)
|
|
# 配置更新成功后,立即更新所有 logger 的级别
|
|
Logger.update_log_levels(config_data["LOG_LEVEL"])
|
|
logger.info("Log levels updated after configuration change.") # 添加日志记录
|
|
return result
|
|
except Exception as e:
|
|
logger.error(f"Error updating config or log levels: {e}", exc_info=True) # 记录详细错误
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
@router.post("/reset", response_model=Dict[str, Any])
|
|
async def reset_config(request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
try:
|
|
return await ConfigService.reset_config()
|
|
except Exception as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|