refactor: 清理后端诊断输出
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from app import state
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def set_task_stage(task_id: str, stage: str, logs: list[str], status: str = "processing") -> None:
|
||||
state.processing_tasks[task_id] = {
|
||||
"logs": logs.copy(),
|
||||
@@ -29,4 +33,4 @@ def apply_history_retention_safely() -> None:
|
||||
try:
|
||||
state.apply_history_retention()
|
||||
except Exception as exc:
|
||||
print(f"自动清理处理历史失败: {exc}")
|
||||
logger.warning("Failed to apply history retention: %s", exc)
|
||||
|
||||
+8
-6
@@ -2,6 +2,7 @@
|
||||
处理历史记录管理模块
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
@@ -12,6 +13,7 @@ from app.config import CACHE_DIR
|
||||
|
||||
|
||||
HISTORY_FILE = CACHE_DIR / "history.json"
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -69,9 +71,9 @@ class HistoryManager:
|
||||
work_path.relative_to(cache_path)
|
||||
shutil.rmtree(work_path)
|
||||
except ValueError:
|
||||
print(f"警告: 尝试删除cache目录外的文件: {work_dir}")
|
||||
logger.warning("Skip deleting path outside cache directory: %s", work_dir)
|
||||
except Exception as e:
|
||||
print(f"删除文件目录失败: {work_dir}, 错误: {e}")
|
||||
logger.warning("Failed to delete work directory %s: %s", work_dir, e)
|
||||
|
||||
def create(self, work_dir: Path, file_count: int, record_id: Optional[str] = None) -> HistoryRecord:
|
||||
"""创建新的历史记录"""
|
||||
@@ -159,7 +161,7 @@ class HistoryManager:
|
||||
logs = [line.strip() for line in content.split('\n') if line.strip()]
|
||||
return logs
|
||||
except Exception as e:
|
||||
print(f"读取日志文件失败: {log_file}, 错误: {e}")
|
||||
logger.warning("Failed to read history log file %s: %s", log_file, e)
|
||||
return []
|
||||
|
||||
def delete(self, record_id: str) -> bool:
|
||||
@@ -238,12 +240,12 @@ class HistoryManager:
|
||||
item_path.unlink()
|
||||
except ValueError:
|
||||
# 路径不在cache目录内,跳过(安全保护)
|
||||
print(f"警告: 跳过cache目录外的文件: {item}")
|
||||
logger.warning("Skip path outside cache directory: %s", item)
|
||||
except Exception as e:
|
||||
# 单个文件/目录删除失败,继续处理其他文件
|
||||
print(f"删除失败: {item}, 错误: {e}")
|
||||
logger.warning("Failed to delete cache item %s: %s", item, e)
|
||||
except Exception as e:
|
||||
# 记录错误但不影响清空历史记录的操作
|
||||
print(f"清空cache目录失败: {e}")
|
||||
logger.warning("Failed to clear cache directory: %s", e)
|
||||
|
||||
return count
|
||||
|
||||
+5
-1
@@ -2,6 +2,7 @@
|
||||
数据处理核心模块
|
||||
"""
|
||||
import chardet
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
@@ -21,6 +22,9 @@ from app.database import DatabaseManager
|
||||
from app.utils.file_dates import DirectoryDateSelection, select_recent_items_by_directory
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProcessLogger:
|
||||
"""处理日志记录器"""
|
||||
|
||||
@@ -53,7 +57,7 @@ class ProcessLogger:
|
||||
f.write(entry + "\n")
|
||||
except Exception as e:
|
||||
# 如果写入失败,至少记录到内存
|
||||
print(f"写入日志文件失败: {e}")
|
||||
logger.warning("Failed to write process log file: %s", e)
|
||||
|
||||
if self.callback:
|
||||
self.callback(entry)
|
||||
|
||||
@@ -830,3 +830,9 @@
|
||||
|
||||
- “添加来源”按钮移动到主键表达式输入区下方右侧;点击后来源列表自动滚动到新增来源。
|
||||
- 验证:`frontend` `npm run build` 通过;构建产物已清理。
|
||||
|
||||
## 2026-06-25:清理后端直接 print 诊断输出
|
||||
|
||||
- `app/history.py`、`app/processor.py`、`app/api/routers/task_runtime.py` 中的异常诊断从直接 `print()` 改为模块级 `logging`,避免后台任务和历史清理在 stdout 中产生零散噪音,同时保留必要警告信息。
|
||||
- `app/main.py` 中的启动横幅 `print()` 保留,仍用于命令行直接启动时提示版本、配置更新时间和访问地址。
|
||||
- 验证:`CapacityReport\.venv\Scripts\python.exe -m compileall -q app` 通过;`frontend` `npx vue-tsc --noEmit --noUnusedLocals --noUnusedParameters` 通过。
|
||||
|
||||
Reference in New Issue
Block a user