feat: unified logging module and remove redundant first_run logic
- Add logger.py with TimedRotatingFileHandler (daily rotation, 30-day retention) - Replace all print() in migrate.py with logging calls - Add logging to sync.py for sync status tracking - Refactor watch.py to use logger.py, remove duplicate first_run branch - All logs now go to logs/app.log consistently - Add logging config section to config.yaml, add logs/ to .gitignore Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
8
sync.py
8
sync.py
@@ -3,9 +3,12 @@ LAN file sync module.
|
||||
Detects file changes, copies from LAN UNC paths to local, and determines which tables need migration.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
|
||||
logger = logging.getLogger('app')
|
||||
|
||||
|
||||
def sync_file(local_path, lan_path):
|
||||
"""Sync a single file from LAN to local.
|
||||
@@ -16,6 +19,7 @@ def sync_file(local_path, lan_path):
|
||||
"""
|
||||
try:
|
||||
if not os.path.exists(lan_path):
|
||||
logger.error(f"LAN 路径不可达: {lan_path}")
|
||||
return 'error', f'LAN path not accessible: {lan_path}'
|
||||
|
||||
lan_mtime = os.path.getmtime(lan_path)
|
||||
@@ -24,6 +28,7 @@ def sync_file(local_path, lan_path):
|
||||
if local_existed:
|
||||
local_mtime = os.path.getmtime(local_path)
|
||||
if local_mtime == lan_mtime:
|
||||
logger.debug(f"跳过 (mtime一致): {os.path.basename(local_path)}")
|
||||
return 'skipped', None
|
||||
|
||||
# Ensure parent directory exists
|
||||
@@ -41,11 +46,14 @@ def sync_file(local_path, lan_path):
|
||||
raise
|
||||
|
||||
if not local_existed:
|
||||
logger.info(f"copied: {os.path.basename(local_path)}")
|
||||
return 'copied', None
|
||||
else:
|
||||
logger.info(f"updated: {os.path.basename(local_path)}")
|
||||
return 'updated', None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"同步失败: {local_path} - {e}")
|
||||
return 'error', str(e)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user