""" 测试 GUI 启动时的日志写入 模拟 GUI 启动 run_cli.py 的过程 """ import os import sys import subprocess import time # 确保在项目根目录 os.chdir(os.path.dirname(os.path.abspath(__file__))) # 首先配置日志系统 print("=" * 60) print("步骤1: 配置日志系统") print("=" * 60) try: import douyin_logging douyin_logging.configure_logging() print("[OK] 日志系统配置成功") except Exception as e: print(f"[ERROR] 日志系统配置失败: {e}") import traceback traceback.print_exc() # 检查日志文件 print("\n" + "=" * 60) print("步骤2: 检查日志文件") print("=" * 60) import path_config log_dir = path_config.get_logs_dir() print(f"日志目录: {log_dir}") print(f"日志目录存在: {os.path.exists(log_dir)}") # 查找今天的日志文件 from datetime import datetime today = datetime.now().strftime("%Y-%m-%d") log_file = os.path.join(log_dir, f"unified_{today}.log") print(f"今天的日志文件: {log_file}") print(f"日志文件存在: {os.path.exists(log_file)}") if os.path.exists(log_file): size_before = os.path.getsize(log_file) print(f"日志文件大小(测试前): {size_before} bytes") # 读取最后几行 with open(log_file, 'r', encoding='utf-8') as f: lines = f.readlines() print(f"日志文件行数: {len(lines)}") if lines: print("最后3行:") for line in lines[-3:]: print(f" {line.rstrip()}") # 测试日志写入 print("\n" + "=" * 60) print("步骤3: 测试日志写入") print("=" * 60) try: import logging logger = logging.getLogger("test.gui") logger.info("这是一条测试日志 - GUI 启动测试") logger.warning("这是一条警告日志 - GUI 启动测试") print("[OK] 日志写入测试完成") except Exception as e: print(f"[ERROR] 日志写入测试失败: {e}") import traceback traceback.print_exc() # 检查日志文件是否更新 if os.path.exists(log_file): time.sleep(0.5) # 等待文件写入 size_after = os.path.getsize(log_file) print(f"\n日志文件大小(测试后): {size_after} bytes") print(f"大小变化: {size_after - size_before} bytes") # 读取新内容 with open(log_file, 'r', encoding='utf-8') as f: lines = f.readlines() print(f"日志文件行数(测试后): {len(lines)}") if len(lines) > len(lines) if 'lines' in locals() else 0: print("新增的最后3行:") for line in lines[-3:]: print(f" {line.rstrip()}") # 测试 fudai_analyse 实例化 print("\n" + "=" * 60) print("步骤4: 测试 fudai_analyse 实例化") print("=" * 60) try: from douyin_fudai import fudai_analyse analyser = fudai_analyse() print("[OK] fudai_analyse 实例化成功") # 测试日志写入 analyser._log_kv("测试", "这是一条来自 fudai_analyse 的测试日志") print("[OK] _log_kv 调用成功") # 检查 _std_logger if hasattr(analyser, '_std_logger') and analyser._std_logger: print(f"[OK] _std_logger 已初始化: {analyser._std_logger}") print(f" Logger 名称: {analyser._std_logger.name}") print(f" Logger 级别: {analyser._std_logger.level}") print(f" Handlers 数量: {len(analyser._std_logger.handlers)}") for i, handler in enumerate(analyser._std_logger.handlers): print(f" Handler {i+1}: {type(handler).__name__}") else: print("[ERROR] _std_logger 未初始化") except Exception as e: print(f"[ERROR] fudai_analyse 实例化失败: {e}") import traceback traceback.print_exc() # 再次检查日志文件 if os.path.exists(log_file): time.sleep(0.5) # 等待文件写入 size_final = os.path.getsize(log_file) print(f"\n日志文件大小(最终): {size_final} bytes") # 读取最新内容 with open(log_file, 'r', encoding='utf-8') as f: lines = f.readlines() print(f"日志文件行数(最终): {len(lines)}") if lines: print("最新的5行:") for line in lines[-5:]: print(f" {line.rstrip()}") print("\n" + "=" * 60) print("测试完成") print("=" * 60)