refactor(server): migrate to zerolog, add request logging middleware

This commit is contained in:
Alexander
2026-04-10 18:19:13 +02:00
parent 3d1eb7bd4b
commit 76bf651742
+5 -4
View File
@@ -3,15 +3,16 @@ package server
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"sync/atomic"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/fujin/anthropic-proxy/internal/auth"
"github.com/fujin/anthropic-proxy/internal/config"
"github.com/fujin/anthropic-proxy/internal/logging"
"github.com/fujin/anthropic-proxy/internal/proxy"
)
@@ -37,6 +38,7 @@ func New(cfg *config.Config, pool *auth.Pool, profile *proxy.SniffedProfile) *Se
engine.Use(gin.Recovery())
engine.Use(corsMiddleware())
engine.Use(s.authMiddleware())
engine.Use(logging.GinRequestLogger())
handler := proxy.HandleMessages(pool, profile, func() *proxy.Sanitizer {
return s.sanitizer.Load()
@@ -50,7 +52,7 @@ func New(cfg *config.Config, pool *auth.Pool, profile *proxy.SniffedProfile) *Se
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
engine.NoRoute(func(c *gin.Context) {
log.Printf("unmatched route: %s %s", c.Request.Method, c.Request.URL.Path)
log.Warn().Str("method", c.Request.Method).Str("path", c.Request.URL.Path).Msg("unmatched route")
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
})
@@ -85,8 +87,7 @@ func (s *Server) handleReload() gin.HandlerFunc {
keys := makeKeySet(cfg.APIKeys)
s.apiKeys.Store(&keys)
log.Printf("config reloaded: %d tool renames, %d system rules, %d body rules, %d api keys",
len(cfg.Sanitize.Tools), len(cfg.Sanitize.System), len(cfg.Sanitize.Body), len(cfg.APIKeys))
log.Info().Int("tool_renames", len(cfg.Sanitize.Tools)).Int("system_rules", len(cfg.Sanitize.System)).Int("body_rules", len(cfg.Sanitize.Body)).Int("api_keys", len(cfg.APIKeys)).Msg("config reloaded")
c.JSON(http.StatusOK, gin.H{
"status": "reloaded",