fix(stats): 将 print() 改为 logging
- 添加 logging 模块导入 - 创建 logger 实例 - 将错误输出从 print() 改为 logger.error() - 保留 __main__ 测试代码中的 print()
This commit is contained in:
@@ -9,11 +9,15 @@ import json
|
||||
import os
|
||||
import tempfile
|
||||
import fcntl
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Any, Optional
|
||||
from collections import defaultdict
|
||||
|
||||
# 配置日志
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 统计数据存储路径
|
||||
STATS_DIR = Path(__file__).parent / "data"
|
||||
STATS_FILE = STATS_DIR / "stats_data.json"
|
||||
@@ -170,8 +174,10 @@ def record_event(
|
||||
_atomic_write(data)
|
||||
|
||||
return True
|
||||
except TypeError:
|
||||
raise # 重新抛出类型错误
|
||||
except Exception as e:
|
||||
print(f"记录事件失败: {e}")
|
||||
logger.error(f"记录事件失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
@@ -301,7 +307,7 @@ def clear_stats(confirm: bool = False) -> bool:
|
||||
STATS_FILE.unlink()
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"清空统计数据失败: {e}")
|
||||
logger.error(f"清空统计数据失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user