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:
2026-05-28 15:52:44 +08:00
parent 4bb50ae9c1
commit 37c0364e7e
19 changed files with 257 additions and 216 deletions
@@ -4,12 +4,15 @@
"""
import base64
import logging
import uuid
from pathlib import Path
from typing import Dict, Any
import httpx
from src.core.config import settings
logger = logging.getLogger(__name__)
class ImageGenerationService:
"""统一的图像生成服务基类"""
@@ -21,9 +24,9 @@ class ImageGenerationService:
async def call_api(self, model: str, payload: dict) -> dict:
"""调用硅基流动 API"""
print(f"[DEBUG] 调用SiliconFlow API: {self.base_url}")
print(f"[DEBUG] 模型: {model}")
print(f"[DEBUG] 请求参数: {payload}")
logger.debug(f"调用SiliconFlow API: {self.base_url}")
logger.debug(f"模型: {model}")
logger.debug(f"请求参数: {payload}")
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.post(
@@ -37,9 +40,9 @@ class ImageGenerationService:
response.raise_for_status()
result = response.json()
print(f"[DEBUG] API响应状态: {response.status_code}")
print(f"[DEBUG] API响应类型: {type(result)}")
print(f"[DEBUG] API响应键: {list(result.keys()) if isinstance(result, dict) else '非字典类型'}")
logger.debug(f"API响应状态: {response.status_code}")
logger.debug(f"API响应类型: {type(result)}")
logger.debug(f"API响应键: {list(result.keys()) if isinstance(result, dict) else '非字典类型'}")
return result
@@ -53,7 +56,7 @@ class ImageGenerationService:
# 移除可能的空白字符
base64_data = base64_data.strip()
print(f"[DEBUG] 开始解码base64数据,长度: {len(base64_data)}")
logger.debug(f"开始解码base64数据,长度: {len(base64_data)}")
# 解码 base64 数据
image_bytes = base64.b64decode(base64_data)
@@ -62,7 +65,7 @@ class ImageGenerationService:
if len(image_bytes) == 0:
raise Exception("解码后的图像数据为空")
print(f"[DEBUG] 图像数据大小: {len(image_bytes)} bytes")
logger.debug(f"图像数据大小: {len(image_bytes)} bytes")
# 确保目录存在
image_dir = Path(settings.generated_images_dir)
@@ -73,13 +76,13 @@ class ImageGenerationService:
with open(image_path, "wb") as f:
f.write(image_bytes)
print(f"[DEBUG] 图像已保存到: {image_path}")
logger.debug(f"图像已保存到: {image_path}")
return str(image_path)
except Exception as e:
print(f"[ERROR] 保存图像失败: {str(e)}")
print(f"[ERROR] base64数据长度: {len(base64_data) if base64_data else 0}")
print(f"[ERROR] base64数据前100字符: {base64_data[:100] if base64_data else 'None'}")
logger.error(f"保存图像失败: {str(e)}")
logger.error(f"base64数据长度: {len(base64_data) if base64_data else 0}")
logger.error(f"base64数据前100字符: {base64_data[:100] if base64_data else 'None'}")
raise Exception(f"保存图像失败: {str(e)}")
def generate_image_id(self) -> str: