Files
Atlas/internal/service/reqlog/logger.go
T
renjue 4009214cbc
CI / docker (push) Successful in 7m4s
实现 Atlas AI 平台一期能力并完成品牌化改造。
引入用户端对话/生图与管理端 Provider 配置,统一 invoke 编排、Gateway、请求日志与模型管理;前端按设计规范拆分布局,Go module 更名为 github.com/rose_cat707/Atlas;生图长任务 SSE 推送;管理端登录防爆破;协议与服务类型联动;Replicate 模型同步修复;用户端创作台 UI 重构;管理端创作历史替代请求日志。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 21:08:57 +08:00

85 lines
1.8 KiB
Go

package reqlog
import (
"context"
"strings"
"time"
"github.com/rose_cat707/Atlas/internal/store"
)
type Logger struct {
store *store.Store
}
func New(st *store.Store) *Logger {
return &Logger{store: st}
}
type Entry struct {
SessionID string
Method string
Path string
Protocol string
ProviderID *int64
Model string
StatusCode int
LatencyMS int
Error string
RequestSummary string
ResponseSummary string
}
func (l *Logger) Log(ctx context.Context, e Entry) {
if l == nil || l.store == nil {
return
}
entry := e
l.store.WriteQueue.Enqueue(func(ctx context.Context, st *store.Store) error {
_, err := st.InsertRequestLog(ctx, &store.RequestLog{
SessionID: entry.SessionID,
Method: entry.Method,
Path: entry.Path,
Protocol: entry.Protocol,
ProviderID: entry.ProviderID,
Model: entry.Model,
StatusCode: entry.StatusCode,
LatencyMS: entry.LatencyMS,
Error: entry.Error,
RequestSummary: entry.RequestSummary,
ResponseSummary: entry.ResponseSummary,
})
return err
})
}
func Summarize(body []byte, max int) string {
s := strings.TrimSpace(string(body))
if len(s) <= max {
return s
}
return s[:max] + "..."
}
func SummarizeSSE(raw string, max int) string {
var b strings.Builder
for _, line := range strings.Split(raw, "\n") {
if strings.HasPrefix(line, "data: ") && !strings.Contains(line, "[DONE]") {
b.WriteString(strings.TrimPrefix(line, "data: "))
b.WriteByte(' ')
}
}
return Summarize([]byte(strings.TrimSpace(b.String())), max)
}
func Since(start time.Time) int {
return int(time.Since(start).Milliseconds())
}
func ProviderIDPtr(id int64) *int64 {
if id <= 0 {
return nil
}
return &id
}