A Simple and Universal Swarm Intelligence Engine, Predicting Anything. 简洁通用的群体智能引擎,预测万物
Python agent-memoryfinancial-forecastingfuture-predictionknowledge-graphllmsmulti-agent-simulationpublic-opinion-analysispython3social-predictionswarm-intelligence
Add UTF-8 encoding support for Windows console in run.py and logger.py to prevent character encoding issues
666ghjf46c1a9ec713
diff --git a/backend/app/utils/logger.py b/backend/app/utils/logger.pyindex a9ad98dc..1978c0b8 100644--- a/backend/app/utils/logger.py+++ b/backend/app/utils/logger.py@@ -4,11 +4,25 @@"""import os+import sysimport loggingfrom datetime import datetimefrom logging.handlers import RotatingFileHandler+def _ensure_utf8_stdout():+ """+ 确保 stdout/stderr 使用 UTF-8 编码+ 解决 Windows 控制台中文乱码问题+ """+ if sys.platform == 'win32':+ # Windows 下重新配置标准输出为 UTF-8+ if hasattr(sys.stdout, 'reconfigure'):+ sys.stdout.reconfigure(encoding='utf-8', errors='replace')+ if hasattr(sys.stderr, 'reconfigure'):+ sys.stderr.reconfigure(encoding='utf-8', errors='replace')++# 日志目录LOG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'logs')@@ -61,7 +75,9 @@ def setup_logger(name: str = 'mirofish', level: int = logging.DEBUG) -> logging.file_handler.setFormatter(detailed_formatter)# 2. 控制台处理器 - 简洁日志(INFO及以上)- console_handler = logging.StreamHandler()+ # 确保 Windows 下使用 UTF-8 编码,避免中文乱码+ _ensure_utf8_stdout()+ console_handler = logging.StreamHandler(sys.stdout)console_handler.setLevel(logging.INFO)console_handler.setFormatter(simple_formatter)diff --git a/backend/run.py b/backend/run.pyindex 78e0ccf4..4e3b04fa 100644--- a/backend/run.py+++ b/backend/run.py@@ -5,6 +5,16 @@ MiroFish Backend 启动入口import osimport sys+# 解决 Windows 控制台中文乱码问题:在所有导入之前设置 UTF-8 编码+if sys.platform == 'win32':+ # 设置环境变量确保 Python 使用 UTF-8+ os.environ.setdefault('PYTHONIOENCODING', 'utf-8')+ # 重新配置标准输出流为 UTF-8+ if hasattr(sys.stdout, 'reconfigure'):+ sys.stdout.reconfigure(encoding='utf-8', errors='replace')+ if hasattr(sys.stderr, 'reconfigure'):+ sys.stderr.reconfigure(encoding='utf-8', errors='replace')+# 添加项目根目录到路径sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))