refactor: replace print() with structured logging across backend
Replace all print()/stderr logging with Python logging module using logger = logging.getLogger(__name__) pattern for consistent log levels and formatting. Extract score conversion to shared score_utils module. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Optional, Callable
|
||||
@@ -13,6 +14,7 @@ from ..core.config import get_settings
|
||||
from ..core.database import get_db
|
||||
from .knowledge_base_service import KnowledgeBaseService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
@@ -27,19 +29,19 @@ class KnowledgeBaseHandler(FileSystemEventHandler):
|
||||
def on_created(self, event):
|
||||
"""处理文件创建事件"""
|
||||
if not event.is_directory and self._is_supported_file(event.src_path):
|
||||
print(f"检测到新文件: {event.src_path}")
|
||||
logger.info(f"检测到新文件: {event.src_path}")
|
||||
self._process_file_async(event.src_path, "created")
|
||||
|
||||
def on_modified(self, event):
|
||||
"""处理文件修改事件"""
|
||||
if not event.is_directory and self._is_supported_file(event.src_path):
|
||||
print(f"检测到文件修改: {event.src_path}")
|
||||
logger.info(f"检测到文件修改: {event.src_path}")
|
||||
self._process_file_async(event.src_path, "modified")
|
||||
|
||||
def on_deleted(self, event):
|
||||
"""处理文件删除事件"""
|
||||
if not event.is_directory and self._is_supported_file(event.src_path):
|
||||
print(f"检测到文件删除: {event.src_path}")
|
||||
logger.info(f"检测到文件删除: {event.src_path}")
|
||||
self._handle_file_deletion(event.src_path)
|
||||
|
||||
def _is_supported_file(self, file_path: str) -> bool:
|
||||
@@ -54,10 +56,10 @@ class KnowledgeBaseHandler(FileSystemEventHandler):
|
||||
time.sleep(1)
|
||||
|
||||
result = self.kb_service.process_file(file_path)
|
||||
print(f"文件处理结果 ({event_type}): {result}")
|
||||
logger.info(f"文件处理结果 ({event_type}): {result}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"处理文件失败: {file_path}, 错误: {str(e)}")
|
||||
logger.error(f"处理文件失败: {file_path}, 错误: {str(e)}")
|
||||
|
||||
# 在后台线程中处理
|
||||
thread = threading.Thread(target=process)
|
||||
@@ -80,12 +82,12 @@ class KnowledgeBaseHandler(FileSystemEventHandler):
|
||||
|
||||
if document:
|
||||
result = self.kb_service.delete_document(document.id)
|
||||
print(f"文件删除处理结果: {result}")
|
||||
logger.info(f"文件删除处理结果: {result}")
|
||||
else:
|
||||
print(f"未找到对应的数据库记录: {file_path}")
|
||||
logger.info(f"未找到对应的数据库记录: {file_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"处理文件删除失败: {file_path}, 错误: {str(e)}")
|
||||
logger.error(f"处理文件删除失败: {file_path}, 错误: {str(e)}")
|
||||
|
||||
|
||||
class FileWatcherService:
|
||||
@@ -99,15 +101,15 @@ class FileWatcherService:
|
||||
def start(self):
|
||||
"""启动文件监控"""
|
||||
if self.is_running:
|
||||
print("文件监控服务已在运行")
|
||||
logger.info("文件监控服务已在运行")
|
||||
return
|
||||
|
||||
if not settings.enable_file_watcher:
|
||||
print("文件监控服务已禁用")
|
||||
logger.info("文件监控服务已禁用")
|
||||
return
|
||||
|
||||
if not self.knowledge_base_dir.exists():
|
||||
print(f"知识库目录不存在: {self.knowledge_base_dir}")
|
||||
logger.info(f"知识库目录不存在: {self.knowledge_base_dir}")
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -130,19 +132,19 @@ class FileWatcherService:
|
||||
self.observer.start()
|
||||
self.is_running = True
|
||||
|
||||
print(f"文件监控服务已启动,监控目录: {self.knowledge_base_dir}")
|
||||
logger.info(f"文件监控服务已启动,监控目录: {self.knowledge_base_dir}")
|
||||
|
||||
# 确保子目录对应的系统知识库存在
|
||||
print("确保系统知识库与目录同步...")
|
||||
logger.info("确保系统知识库与目录同步...")
|
||||
kb_service.ensure_system_knowledge_bases()
|
||||
|
||||
# 执行初始扫描
|
||||
print("执行初始知识库扫描...")
|
||||
logger.info("执行初始知识库扫描...")
|
||||
scan_result = kb_service.scan_directory()
|
||||
print(f"初始扫描结果: {scan_result}")
|
||||
logger.info(f"初始扫描结果: {scan_result}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"启动文件监控服务失败: {str(e)}")
|
||||
logger.error(f"启动文件监控服务失败: {str(e)}")
|
||||
self.is_running = False
|
||||
|
||||
def stop(self):
|
||||
@@ -151,7 +153,7 @@ class FileWatcherService:
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
self.is_running = False
|
||||
print("文件监控服务已停止")
|
||||
logger.info("文件监控服务已停止")
|
||||
|
||||
def is_active(self) -> bool:
|
||||
"""检查监控服务是否活跃"""
|
||||
|
||||
Reference in New Issue
Block a user