34feac02 by shady

增加部分log日志文件处理

1 parent 41026f54
...@@ -44,10 +44,65 @@ def init_logging(log_level=logging.INFO): ...@@ -44,10 +44,65 @@ def init_logging(log_level=logging.INFO):
44 创建logs目录并配置基本的文件日志记录 44 创建logs目录并配置基本的文件日志记录
45 """ 45 """
46 try: 46 try:
47 # 获取脚本所在目录 47 # 智能选择logs目录 - 与images目录使用相同的逻辑
48 script_dir = Path(__file__).parent 48 def get_logs_directory() -> Path:
49 """获取可用的logs目录,支持开发环境和打包环境"""
50 def get_candidate_logs_paths():
51 """获取logs候选路径列表"""
52 system = platform.system()
53 candidates = []
54
55 # 1. 优先尝试:当前目录的logs文件夹
56 if getattr(sys, 'frozen', False):
57 # 打包环境:使用可执行文件所在目录
58 candidates.append(Path(sys.executable).parent / "logs")
59 else:
60 # 开发环境:使用脚本所在目录
61 candidates.append(Path(__file__).parent / "logs")
62
63 # 2. 备选方案:用户目录
64 if system == "Darwin": # macOS
65 candidates.append(Path.home() / "Library/Application Support/ZB100ImageGenerator/logs")
66 candidates.append(Path.home() / "Documents/ZB100ImageGenerator/logs")
67 elif system == "Windows":
68 candidates.append(Path(os.environ.get("APPDATA", "")) / "ZB100ImageGenerator/logs")
69 candidates.append(Path.home() / "Documents/ZB100ImageGenerator/logs")
70 else: # Linux
71 candidates.append(Path.home() / ".config/zb100imagegenerator/logs")
72 candidates.append(Path.home() / "Documents/ZB100ImageGenerator/logs")
73
74 return candidates
75
76 def test_logs_write_access(path: Path) -> bool:
77 """测试logs路径是否有写入权限"""
78 try:
79 # 尝试创建目录
80 path.mkdir(parents=True, exist_ok=True)
81
82 # 测试写入权限
83 test_file = path / ".write_test"
84 test_file.write_text("test")
85 test_file.unlink() # 删除测试文件
86 return True
87 except Exception:
88 return False
89
90 # 测试候选路径
91 for candidate_path in get_candidate_logs_paths():
92 if test_logs_write_access(candidate_path):
93 print(f"使用logs目录: {candidate_path}")
94 return candidate_path
95
96 # 所有路径都失败,使用临时目录
97 fallback_path = Path(tempfile.gettempdir()) / "ZB100ImageGenerator_logs"
98 print(f"警告: 所有logs路径都不可用,使用临时目录: {fallback_path}")
99 fallback_path.mkdir(exist_ok=True)
100 return fallback_path
101
102 logs_dir = get_logs_directory()
49 103
50 # 尝试加载日志配置 104 # 尝试加载日志配置
105 script_dir = Path(__file__).parent if not getattr(sys, 'frozen', False) else Path(sys.executable).parent
51 config_path = script_dir / "config.json" 106 config_path = script_dir / "config.json"
52 logging_config = { 107 logging_config = {
53 "enabled": True, 108 "enabled": True,
...@@ -72,9 +127,8 @@ def init_logging(log_level=logging.INFO): ...@@ -72,9 +127,8 @@ def init_logging(log_level=logging.INFO):
72 level_str = logging_config.get("level", "INFO").upper() 127 level_str = logging_config.get("level", "INFO").upper()
73 log_level = getattr(logging, level_str, logging.INFO) 128 log_level = getattr(logging, level_str, logging.INFO)
74 129
75 # 创建logs目录 130 # 确保logs目录存在
76 logs_dir = script_dir / "logs" 131 logs_dir.mkdir(parents=True, exist_ok=True)
77 logs_dir.mkdir(exist_ok=True)
78 132
79 # 配置日志文件路径 133 # 配置日志文件路径
80 log_file = logs_dir / "app.log" 134 log_file = logs_dir / "app.log"
......