refactor: 全面集成 slog 日志到 handler、service、middleware

- cmd/main.go: 集成 logger.Init 和日志中间件,替换标准 log 包
- handler 层: 5xx 错误记录完整日志,返回通用消息(防内部信息泄露)
- service 层: LLM/图片生成/存储/认证等关键操作补充结构化日志
- auth 中间件: 记录认证失败原因
- generate.go: 后台管线任务使用带 task_id 的 logger
This commit is contained in:
2026-05-25 14:31:04 +08:00
parent 681bc8f472
commit 25c11ed649
13 changed files with 159 additions and 39 deletions
+7 -5
View File
@@ -4,6 +4,7 @@ import (
"encoding/base64"
"net/http"
"gen2d/internal/logger"
"gen2d/internal/model"
"gen2d/internal/service"
@@ -12,9 +13,9 @@ import (
// EditImageRequest 图片编辑请求。
type EditImageRequest struct {
Image string `json:"image" binding:"required"` // 底图 base64 编码
Prompt string `json:"prompt" binding:"required"` // 编辑指令
Count int `json:"count"` // 生成数量,默认 1
Image string `json:"image" binding:"required"` // 底图 base64 编码
Prompt string `json:"prompt" binding:"required"` // 编辑指令
Count int `json:"count"` // 生成数量,默认 1
}
// editAssetResponse 编辑结果素材(返回 base64)。
@@ -38,7 +39,7 @@ func EditImage(c *gin.Context) {
imageData, err := base64.StdEncoding.DecodeString(req.Image)
if err != nil {
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "图片 base64 解码失败: "+err.Error()))
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "图片 base64 解码失败"))
return
}
@@ -49,7 +50,8 @@ func EditImage(c *gin.Context) {
images, err := service.EditImages(c.Request.Context(), imageData, req.Prompt, count)
if err != nil {
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "图片编辑失败: "+err.Error()))
logger.FromCtx(c.Request.Context()).Error("图片编辑失败", "error", err)
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "图片编辑失败"))
return
}