Skip to content

Commit 868c496

Browse files
committed
项目:修复日志显示问题
1 parent edbbc0b commit 868c496

File tree

5 files changed

+113
-29
lines changed

5 files changed

+113
-29
lines changed

app/api/chat_routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from flask import Blueprint, jsonify, request
77
from app.auth import require_api_key
8-
from app.logs import logger
8+
from app.unified_logger import logger
99
from app.wechat import wechat_manager
1010
import time
1111

app/api/message_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from app.wechat import wechat_manager
1212
import config_manager
1313

14-
# 配置日志
15-
logger = logging.getLogger(__name__)
14+
# 使用统一日志系统
15+
from app.unified_logger import logger
1616

1717
# 创建蓝图
1818
message_bp = Blueprint('message', __name__)

app/api/plugin_routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from app import plugin_manager
1515
import config_manager
1616

17-
# 配置日志
18-
logger = logging.getLogger(__name__)
17+
# 使用统一日志系统
18+
from app.unified_logger import logger
1919

2020
# 创建蓝图
2121
plugin_bp = Blueprint('plugins', __name__)

app/app_ui.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ def check_wxauto_status(self):
476476
def check_wxautox_status(self):
477477
"""检查wxautox库的可用状态(能否成功导入)"""
478478
try:
479+
# 初始化日志标志
480+
if not hasattr(self, '_wxautox_status_logged'):
481+
self._wxautox_status_logged = {}
482+
479483
# 在打包环境中,避免使用subprocess检查,防止冲突
480484
is_frozen = getattr(sys, 'frozen', False)
481485
if is_frozen:
@@ -484,21 +488,35 @@ def check_wxautox_status(self):
484488
import importlib.util
485489
spec = importlib.util.find_spec('wxautox')
486490
if spec is not None:
487-
self.wxautox_status.config(text="已打包", style="Green.TLabel")
488-
self.add_log("wxautox库在打包环境中可用,准备检测激活状态")
489-
# 在打包环境中,延迟检测激活状态
490-
self.root.after(1000, self._check_wxautox_activation_once)
491+
# 检查状态是否已经更新,避免重复日志
492+
current_status = self.wxautox_status.cget("text")
493+
if current_status != "已打包":
494+
self.wxautox_status.config(text="已打包", style="Green.TLabel")
495+
if not self._wxautox_status_logged.get('packed_available', False):
496+
self.add_log("wxautox库在打包环境中可用,准备检测激活状态")
497+
self._wxautox_status_logged['packed_available'] = True
498+
# 在打包环境中,延迟检测激活状态
499+
self.root.after(1000, self._check_wxautox_activation_once)
491500
return True
492501
else:
493-
self.wxautox_status.config(text="未打包", style="Red.TLabel")
494-
if hasattr(self, 'wxautox_activation_status'):
495-
self.wxautox_activation_status.config(text="未打包", style="Red.TLabel")
502+
current_status = self.wxautox_status.cget("text")
503+
if current_status != "未打包":
504+
self.wxautox_status.config(text="未打包", style="Red.TLabel")
505+
if hasattr(self, 'wxautox_activation_status'):
506+
self.wxautox_activation_status.config(text="未打包", style="Red.TLabel")
507+
# 重置日志标志
508+
self._wxautox_status_logged = {}
496509
return False
497510
except Exception:
498-
self.wxautox_status.config(text="已打包", style="Green.TLabel")
499-
self.add_log("wxautox库在打包环境中假设可用,准备检测激活状态")
500-
# 在打包环境中,延迟检测激活状态
501-
self.root.after(1000, self._check_wxautox_activation_once)
511+
# 检查状态是否已经更新,避免重复日志
512+
current_status = self.wxautox_status.cget("text")
513+
if current_status != "已打包":
514+
self.wxautox_status.config(text="已打包", style="Green.TLabel")
515+
if not self._wxautox_status_logged.get('packed_assumed', False):
516+
self.add_log("wxautox库在打包环境中假设可用,准备检测激活状态")
517+
self._wxautox_status_logged['packed_assumed'] = True
518+
# 在打包环境中,延迟检测激活状态
519+
self.root.after(1000, self._check_wxautox_activation_once)
502520
return True # 假设可用,避免阻止启动
503521
else:
504522
# 在开发环境中,使用subprocess来检查wxautox,避免影响主进程
@@ -513,19 +531,28 @@ def check_wxautox_status(self):
513531
)
514532

515533
if result.returncode == 0 and "wxautox_available" in result.stdout:
516-
self.wxautox_status.config(text="可用", style="Green.TLabel")
517-
# 获取并显示版本号
518-
version = self.get_package_version('wxautox')
519-
self.wxautox_version.config(text=version)
520-
self.add_log("wxautox库检测为可用,将在微信初始化成功后检测激活状态")
534+
# 检查状态是否已经更新,避免重复日志
535+
current_status = self.wxautox_status.cget("text")
536+
if current_status != "可用":
537+
self.wxautox_status.config(text="可用", style="Green.TLabel")
538+
# 获取并显示版本号
539+
version = self.get_package_version('wxautox')
540+
self.wxautox_version.config(text=version)
541+
if not self._wxautox_status_logged.get('dev_available', False):
542+
self.add_log("wxautox库检测为可用,将在微信初始化成功后检测激活状态")
543+
self._wxautox_status_logged['dev_available'] = True
521544
# 不立即检测激活状态,等待微信初始化成功后再检测
522545
return True
523546
else:
524-
self.wxautox_status.config(text="不可用", style="Red.TLabel")
525-
self.wxautox_version.config(text="")
526-
# 更新激活状态显示
527-
if hasattr(self, 'wxautox_activation_status'):
528-
self.wxautox_activation_status.config(text="未激活", style="Red.TLabel")
547+
current_status = self.wxautox_status.cget("text")
548+
if current_status != "不可用":
549+
self.wxautox_status.config(text="不可用", style="Red.TLabel")
550+
self.wxautox_version.config(text="")
551+
# 更新激活状态显示
552+
if hasattr(self, 'wxautox_activation_status'):
553+
self.wxautox_activation_status.config(text="未激活", style="Red.TLabel")
554+
# 重置日志标志
555+
self._wxautox_status_logged = {}
529556
return False
530557
except subprocess.TimeoutExpired:
531558
self.wxautox_status.config(text="检查超时", style="Red.TLabel")

app/unified_logger.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,64 @@ def log_debug(lib_name: str, message: str):
285285
unified_logger.debug(lib_name, message)
286286

287287

288-
# 安全的日志适配器 - 避免递归调用但提供基本功能
288+
# 统一日志适配器 - 使用真正的统一日志系统
289+
class UnifiedLoggerAdapter:
290+
"""统一日志适配器,使用真正的统一日志系统"""
291+
292+
def __init__(self, lib_name: str = "Flask"):
293+
self.lib_name = lib_name
294+
295+
def set_lib_name(self, lib_name: str):
296+
"""设置库名称"""
297+
self.lib_name = lib_name
298+
299+
def info(self, message: str):
300+
"""INFO日志"""
301+
try:
302+
unified_logger.info(self.lib_name, message)
303+
except:
304+
# 如果统一日志系统失败,回退到简单打印
305+
try:
306+
print(f"[{self.lib_name}] INFO: {message}")
307+
except:
308+
pass
309+
310+
def warning(self, message: str):
311+
"""WARNING日志"""
312+
try:
313+
unified_logger.warning(self.lib_name, message)
314+
except:
315+
try:
316+
print(f"[{self.lib_name}] WARNING: {message}")
317+
except:
318+
pass
319+
320+
def error(self, message: str, exc_info=None):
321+
"""ERROR日志"""
322+
try:
323+
if exc_info:
324+
import traceback
325+
tb_str = traceback.format_exc()
326+
message = f"{message}\n{tb_str}"
327+
unified_logger.error(self.lib_name, message)
328+
except:
329+
try:
330+
print(f"[{self.lib_name}] ERROR: {message}")
331+
except:
332+
pass
333+
334+
def debug(self, message: str):
335+
"""DEBUG日志"""
336+
try:
337+
unified_logger.debug(self.lib_name, message)
338+
except:
339+
try:
340+
print(f"[{self.lib_name}] DEBUG: {message}")
341+
except:
342+
pass
343+
344+
345+
# 安全的日志适配器 - 避免递归调用但提供基本功能(用于特殊情况)
289346
class SafeLoggerAdapter:
290347
"""安全的日志适配器,避免递归调用但提供基本日志功能"""
291348

@@ -329,5 +386,5 @@ def debug(self, message: str):
329386
pass
330387

331388

332-
# 创建安全的适配器实例
333-
logger = SafeLoggerAdapter(Config.WECHAT_LIB)
389+
# 创建统一日志适配器实例 - 用于API路由
390+
logger = UnifiedLoggerAdapter("Flask")

0 commit comments

Comments
 (0)