diff --git a/Application/web.py b/Application/web.py index 4b8cb0d..6680dae 100644 --- a/Application/web.py +++ b/Application/web.py @@ -76,7 +76,7 @@ async def root(): @app.exception_handler(Exception) async def global_exception_handler(request, exc): """全局异常处理""" - config.Log("Error", f"未捕获的异常: {exc}", exc_info=True) + config.Log("Error", f"未捕获的异常: {exc}\n{format_traceback_info()}") return JSONResponse( status_code=500, content={"error": "Internal Server Error", "detail": str(exc)} diff --git a/CoreModules/database.py b/CoreModules/database.py index c564760..ebbc6e4 100644 --- a/CoreModules/database.py +++ b/CoreModules/database.py @@ -1,8 +1,6 @@ """SQLite数据库操作模块 - 使用标准库sqlite3""" import sqlite3 -import json -import time -from typing import * +from ..Convention.Runtime.Config import * from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor from ..Convention.Runtime.Architecture import Architecture from ..Convention.Runtime.File import ToolFile @@ -51,7 +49,7 @@ class Database: config.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接成功: {self.db_path}{ConsoleFrontColor.RESET}") except Exception as e: - config.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}", exc_info=True) + config.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}") raise return self._conn diff --git a/CoreModules/middleware.py b/CoreModules/middleware.py index 83650d4..068df6b 100644 --- a/CoreModules/middleware.py +++ b/CoreModules/middleware.py @@ -3,7 +3,7 @@ import asyncio from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import Response -from ..Convention.Runtime.GlobalConfig import ProjectConfig +from ..Convention.Runtime.GlobalConfig import * config = ProjectConfig() MAX_CONCURRENT_REQUESTS = config.FindItem("max_concurrent_requests", 100) @@ -24,7 +24,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware): response = await call_next(request) return response except Exception as e: - config.Log("Error", f"请求处理错误: {e}", exc_info=True) + config.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}") return Response( content='{"error": "Internal Server Error"}', status_code=500, diff --git a/CoreModules/plugin_interface.py b/CoreModules/plugin_interface.py index aaad3b1..26f64eb 100644 --- a/CoreModules/plugin_interface.py +++ b/CoreModules/plugin_interface.py @@ -30,7 +30,7 @@ class PluginInterface(ABC): chat_id: 会话ID user_id: 用户ID ''' - config.Log("Warning", f"插件{self.__class__.__name__}未实现callback方法") + config.Log("Warning", f"{ConsoleFrontColor.YELLOW}插件{self.__class__.__name__}未实现callback方法{ConsoleFrontColor.RESET}") return "" def execute(self, path:str) -> Optional[APIRouter]: @@ -45,9 +45,13 @@ class PluginInterface(ABC): db = get_db() db_model = self.register_db_model() if db_model: - db.define_table(db_model.table_name) - for name, field_def in db_model.column_defs.items(): - db.define_column(db_model.table_name, name, field_def) + cursor = db.conn.cursor() + sql = f"CREATE TABLE IF NOT EXISTS {db_model.table_name} ({', '.join([f'{name} {field_def}' for name, field_def in db_model.column_defs.items()])})" + config.Log("Info", f"{ConsoleFrontColor.LIGHTMAGENTA_EX}为表 {db_model.table_name} 创建: {sql}{ConsoleFrontColor.RESET}") + try: + cursor.execute(sql) + except Exception as e: + config.Log("Error", f"{ConsoleFrontColor.RED}为表 {db_model.table_name} 创建失败: {e}{ConsoleFrontColor.RESET}") return router @@ -77,16 +81,16 @@ class PluginInterface(ABC): 将插件注册, 使其可以被命令匹配 ''' if command in PluginInterface.plugin_instances: - config.Log("Warning", f"插件{PluginInterface.plugin_instances[command].__class__.__name__}已注册命令{command}, 将被新插件{self.__class__.__name__}覆盖") + config.Log("Warning", f"{ConsoleFrontColor.YELLOW}插件{PluginInterface.plugin_instances[command].__class__.__name__}已注册命令{command}, 将被新插件{self.__class__.__name__}覆盖{ConsoleFrontColor.RESET}") else: config.Log("Info", f"插件{self.__class__.__name__}已注册命令{command}") PluginInterface.plugin_instances[command] = self - def register_db_model(self) -> DatabaseModel: + def register_db_model(self) -> Optional[DatabaseModel]: ''' 继承后重写该方法注册数据库模型 ''' - return DatabaseModel() + return None def is_enable_plugin(self) -> bool: ''' diff --git a/CoreRouters/callback.py b/CoreRouters/callback.py index 2835897..405438e 100644 --- a/CoreRouters/callback.py +++ b/CoreRouters/callback.py @@ -1,5 +1,5 @@ """Callback路由处理""" -from ..Convention.Runtime.GlobalConfig import ProjectConfig +from ..Convention.Runtime.GlobalConfig import * from fastapi import APIRouter, Request from fastapi.responses import JSONResponse @@ -103,6 +103,6 @@ async def handle_command(command: str, message: str, else: return f"❌ 未识别指令: {command}" except Exception as e: - config.Log("Error", f"处理指令异常: {e}", exc_info=True) + config.Log("Error", f"{ConsoleFrontColor.RED}处理指令异常: {e}{ConsoleFrontColor.RESET}") return f"❌ 处理指令时出错: {str(e)}" diff --git a/CoreRouters/health.py b/CoreRouters/health.py index 35e64c7..0d773ab 100644 --- a/CoreRouters/health.py +++ b/CoreRouters/health.py @@ -53,7 +53,7 @@ async def system_stats(): } }) except Exception as e: - config.Log("Error", f"{ConsoleFrontColor.RED}获取系统统计失败: {e}{ConsoleFrontColor.RESET}", exc_info=True) + config.Log("Error", f"{ConsoleFrontColor.RED}获取系统统计失败: {e}{ConsoleFrontColor.RESET}") return JSONResponse( status_code=500, content={"error": str(e)}