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:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
知识库管理服务
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
@@ -15,6 +16,8 @@ from ..models.user import User
|
||||
from ..core.config import get_settings
|
||||
from .document_service import DocumentService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
@@ -106,7 +109,7 @@ class KnowledgeBaseService:
|
||||
existing.user_id = admin.id
|
||||
self.db.commit()
|
||||
self.db.refresh(existing)
|
||||
print(f"[KB] 升级为系统知识库: {name} (id={existing.id})")
|
||||
logger.info(f"升级为系统知识库: {name} (id={existing.id})")
|
||||
return existing.id
|
||||
|
||||
# 找到 admin 用户(或任意 superuser)作为 owner
|
||||
@@ -123,7 +126,7 @@ class KnowledgeBaseService:
|
||||
self.db.add(kb)
|
||||
self.db.commit()
|
||||
self.db.refresh(kb)
|
||||
print(f"[KB] 自动创建系统知识库: {name} (id={kb.id})")
|
||||
logger.info(f"自动创建系统知识库: {name} (id={kb.id})")
|
||||
return kb.id
|
||||
|
||||
def _resolve_knowledge_base(self, file_path: Path) -> Optional[int]:
|
||||
@@ -249,13 +252,13 @@ class KnowledgeBaseService:
|
||||
loop.run_until_complete(self.document_service.process_document(document.id))
|
||||
loop.close()
|
||||
except Exception as e:
|
||||
print(f"后台处理文档 {document.id} 失败: {e}")
|
||||
logger.error(f"后台处理文档 {document.id} 失败: {e}")
|
||||
|
||||
thread = threading.Thread(target=process_in_background, daemon=True)
|
||||
thread.start()
|
||||
except Exception as e:
|
||||
print(f"启动文档处理线程失败: {e}")
|
||||
|
||||
logger.error(f"启动文档处理线程失败: {e}")
|
||||
|
||||
return {"status": "new", "document_id": document.id, "message": "文档创建成功,正在处理中"}
|
||||
|
||||
except Exception as e:
|
||||
@@ -287,13 +290,13 @@ class KnowledgeBaseService:
|
||||
loop.run_until_complete(self.document_service.process_document(document.id))
|
||||
loop.close()
|
||||
except Exception as e:
|
||||
print(f"后台处理文档 {document.id} 失败: {e}")
|
||||
logger.error(f"后台处理文档 {document.id} 失败: {e}")
|
||||
|
||||
thread = threading.Thread(target=process_in_background, daemon=True)
|
||||
thread.start()
|
||||
except Exception as e:
|
||||
print(f"启动文档处理线程失败: {e}")
|
||||
|
||||
logger.error(f"启动文档处理线程失败: {e}")
|
||||
|
||||
return {"status": "updated", "document_id": document.id, "message": "文档更新成功,正在重新处理中"}
|
||||
|
||||
except Exception as e:
|
||||
@@ -482,7 +485,7 @@ class KnowledgeBaseService:
|
||||
all_files = [f for f in directory.rglob("*") if f.is_file() and self._is_supported_file(f)]
|
||||
total_files = len(all_files)
|
||||
|
||||
print(f" 找到 {total_files} 个支持的文件,开始处理...")
|
||||
logger.info(f"找到 {total_files} 个支持的文件,开始处理...")
|
||||
|
||||
for idx, file_path in enumerate(all_files, 1):
|
||||
try:
|
||||
@@ -506,7 +509,7 @@ class KnowledgeBaseService:
|
||||
# 显示进度(每10个文件或最后一个文件时显示)
|
||||
if idx % 10 == 0 or idx == total_files:
|
||||
percentage = (idx * 100) // total_files if total_files > 0 else 0
|
||||
print(f"\r 处理进度: {idx}/{total_files} ({percentage}%)", end="", flush=True)
|
||||
logger.info(f"处理进度: {idx}/{total_files} ({percentage}%)")
|
||||
|
||||
if existing_doc:
|
||||
# 检查是否需要更新
|
||||
@@ -549,6 +552,6 @@ class KnowledgeBaseService:
|
||||
"error": str(e)
|
||||
})
|
||||
|
||||
print() # 换行
|
||||
logger.info("文件处理完成")
|
||||
results["success"] = len(results["errors"]) == 0
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user