Refactor logging system to use unified log_utils module and enhance logging messages across multiple scripts

This commit is contained in:
Misaka_Company
2026-01-12 13:55:28 +08:00
parent 83c90f0161
commit 4721634b81
7 changed files with 227 additions and 246 deletions

View File

@@ -4,7 +4,10 @@ import shutil
import urllib
from sqlalchemy import create_engine, text
from config import DB_CONFIG, MIGRATION_TASKS, TEMP_DIR
import ntfy_utils # 确保该文件在同一目录下
from log_utils import LoggerManager, log_start, log_skip, log_processing, log_success, log_warning, log_error, log_complete
# 初始化日志管理器
LoggerManager("migration", log_prefix="migration")
# ==========================================
# 1. 脚本配置 (Configuration)
@@ -67,7 +70,7 @@ def run_migration():
sync_count = 0
error_count = 0
print(f"🚀 开始增量同步任务 (强制更新={FORCE_UPDATE})")
log_start(f"增量同步任务 (强制更新={FORCE_UPDATE})")
for task in MIGRATION_TASKS:
remote_path = task['file_path']
@@ -77,8 +80,7 @@ def run_migration():
# 1. 检查源文件
if not os.path.exists(remote_path):
msg = f"远程文件未找到: {remote_path}"
print(f"{msg}")
ntfy_utils.send_error(msg)
log_error(msg)
continue
# 2. 增量判定
@@ -86,10 +88,10 @@ def run_migration():
local_mtime = get_file_mtime(local_path)
if not FORCE_UPDATE and os.path.exists(local_path) and remote_mtime <= local_mtime:
print(f"⏭️ 跳过: {filename} (文件未变更)")
log_skip(f"{filename} (文件未变更)")
continue
print(f"🔄 正在处理: {filename} ...")
log_processing(f"正在处理: {filename} ...")
try:
# 3. 复制文件到本地 temp
@@ -112,7 +114,7 @@ def run_migration():
missing = [c for c in source_cols if c not in df.columns]
if missing:
print(f" ⚠️ Sheet[{sheet_name}] 缺失列: {missing}")
log_warning(f"Sheet[{sheet_name}] 缺失列: {missing}")
continue
# 提取并重命名
@@ -151,23 +153,19 @@ def run_migration():
chunksize=1000
)
print(f"成功同步: {len(final_df)} 行记录")
log_success(f"成功同步: {len(final_df)} 行记录")
sync_count += 1
else:
print(f" ⚠️ 警告: 文件内容为空或格式不符")
log_warning("文件内容为空或格式不符")
except Exception as e:
error_msg = f"文件 [{filename}] 处理失败: {str(e)}"
print(f"{error_msg}")
ntfy_utils.send_error(error_msg)
log_error(error_msg)
error_count += 1
# 结束汇总
summary = f"同步完成: 成功 {sync_count} 个文件, 失败 {error_count} 个文件。"
print(f"\n🏁 {summary}")
if sync_count > 0:
# 只有在有实际更新时才发送成功通知
ntfy_utils.send_ntfy(summary, title="📊 数据迁移报告", tags=["package"])
log_complete(f"同步完成: 成功 {sync_count} 个文件, 失败 {error_count} 个文件")
if __name__ == "__main__":
run_migration()