#!/usr/bin/env python3 """项目自检脚本:检查 action_logger 初始化与 analyser._log_kv 绑定。 用法: 在与项目相同的 Python 环境中运行: python tools/check_action_logger.py """ import logging import time import traceback from pathlib import Path import sys PROJECT_ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(PROJECT_ROOT)) from douyin.logging import action_logger, douyin_logging_wrapper from douyin.core.douyin_fudai import fudai_analyse from douyin.core import path_config def safe_repr(obj): try: return repr(obj) except Exception: return "" def main(): print("=== action_logger / handler 状态 ===") try: al = logging.getLogger("action_logger") handlers = al.handlers print( "logger 'action_logger' handlers:", [(type(h).__name__, getattr(h, "baseFilename", None)) for h in handlers], ) except Exception: print("获取 action_logger 失败:") traceback.print_exc() print("\n=== action_logger 模块队列/监听器 ===") try: q = getattr(action_logger, "_log_queue", None) l = getattr(action_logger, "_queue_listener", None) print("_log_queue:", type(q).__name__ if q is not None else None) print("_queue_listener:", type(l).__name__ if l is not None else None) if l is not None: # QueueListener has method 'is_alive' if it's a Thread subclass; otherwise skip alive = getattr(l, "is_alive", lambda: False)() print("listener is_alive:", alive) except Exception: traceback.print_exc() print("\n=== actions.log 路径与权限 ===") try: path = path_config.get_actions_log_path() print("actions.log path:", path) try: with open(path, "a", encoding="utf-8") as f: f.write(f"#check {time.time()}\n") print("已向 actions.log 追加一行(权限与磁盘检查通过)") except Exception as e: print("写入 actions.log 失败:", e) except Exception: traceback.print_exc() print("\n=== analyser._log_kv 绑定检查与试写 ===") try: a = fudai_analyse() print("analyser._log_kv repr:", safe_repr(a._log_kv)) try: a._log_kv("check_point", "self_check", detail="test_write") print( "已调用 analyser._log_kv(...). 请查看 actions.log 是否包含该条目(或等待几秒队列刷新)" ) except Exception as e: print("调用 analyser._log_kv 失败:", e) except Exception: traceback.print_exc() print("\n检查完成。") if __name__ == "__main__": main()