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:
@@ -7,11 +7,11 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gen2d/internal/config"
|
||||
"gen2d/internal/logger"
|
||||
|
||||
"github.com/cloudwego/eino/compose"
|
||||
)
|
||||
@@ -130,8 +130,10 @@ type chatResponse struct {
|
||||
|
||||
// callLLMRefine 调用 LLM 生成规范化提示词。未配置 API key 时回退到模板生成。
|
||||
func callLLMRefine(ctx context.Context, metaPrompt string) (PromptAgentOutput, error) {
|
||||
l := logger.FromCtx(ctx)
|
||||
|
||||
if llmCfg.APIKey == "" {
|
||||
log.Println("[prompt_agent] LLM API key not configured, using template fallback")
|
||||
l.Warn("LLM API key not configured, using template fallback")
|
||||
return fallbackRefine(metaPrompt), nil
|
||||
}
|
||||
|
||||
@@ -140,12 +142,14 @@ func callLLMRefine(ctx context.Context, metaPrompt string) (PromptAgentOutput, e
|
||||
{Role: "user", Content: metaPrompt},
|
||||
}
|
||||
|
||||
l.Info("calling LLM API", "model", llmCfg.Model)
|
||||
result, err := chatCompletion(ctx, messages)
|
||||
if err != nil {
|
||||
log.Printf("[prompt_agent] LLM API call failed: %v, using template fallback", err)
|
||||
l.Error("LLM API call failed, using template fallback", "error", err)
|
||||
return fallbackRefine(metaPrompt), nil
|
||||
}
|
||||
|
||||
l.Info("LLM API succeeded", "response_length", len(result))
|
||||
return PromptAgentOutput{
|
||||
Prompt: result,
|
||||
RawText: result,
|
||||
@@ -175,6 +179,8 @@ func chatCompletion(ctx context.Context, messages []chatMessage) (string, error)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+llmCfg.APIKey)
|
||||
|
||||
l := logger.FromCtx(ctx)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("send request: %w", err)
|
||||
@@ -183,6 +189,7 @@ func chatCompletion(ctx context.Context, messages []chatMessage) (string, error)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
l.Error("LLM API error", "status", resp.StatusCode, "body", string(b))
|
||||
return "", fmt.Errorf("llm api error %d: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user