refactor(main): migrate to zerolog structured logging

This commit is contained in:
Alexander
2026-04-10 18:19:13 +02:00
parent bfcbe0b37d
commit 3d1eb7bd4b
+22 -14
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@@ -12,18 +11,27 @@ import (
"github.com/fujin/anthropic-proxy/internal/auth" "github.com/fujin/anthropic-proxy/internal/auth"
"github.com/fujin/anthropic-proxy/internal/config" "github.com/fujin/anthropic-proxy/internal/config"
"github.com/fujin/anthropic-proxy/internal/logging"
"github.com/fujin/anthropic-proxy/internal/proxy" "github.com/fujin/anthropic-proxy/internal/proxy"
"github.com/fujin/anthropic-proxy/internal/server" "github.com/fujin/anthropic-proxy/internal/server"
"github.com/rs/zerolog/log"
) )
func run() error { func run() error {
log.SetFlags(log.LstdFlags)
cfg, err := config.Load("config.yaml") cfg, err := config.Load("config.yaml")
if err != nil { if err != nil {
return fmt.Errorf("load config: %w", err) return fmt.Errorf("load config: %w", err)
} }
logging.Setup(logging.Config{
Level: cfg.Logging.Level,
File: cfg.Logging.File,
MaxSizeMB: cfg.Logging.MaxSizeMB,
MaxBackups: cfg.Logging.MaxBackups,
MaxAgeDays: cfg.Logging.MaxAgeDays,
Compress: cfg.Logging.Compress,
})
// Load credentials from ~/.claude/.credentials.json // Load credentials from ~/.claude/.credentials.json
creds, err := config.LoadDefaultCredentials() creds, err := config.LoadDefaultCredentials()
if err != nil { if err != nil {
@@ -35,15 +43,15 @@ func run() error {
cred = creds[0] cred = creds[0]
// If token is expired, try refresh first // If token is expired, try refresh first
if !cred.ExpiresAt.IsZero() && time.Now().After(cred.ExpiresAt) { if !cred.ExpiresAt.IsZero() && time.Now().After(cred.ExpiresAt) {
log.Printf("token expired, attempting refresh...") log.Info().Msg("token expired, attempting refresh")
refreshCtx, refreshCancel := context.WithTimeout(context.Background(), 15*time.Second) refreshCtx, refreshCancel := context.WithTimeout(context.Background(), 15*time.Second)
refreshErr := auth.RefreshToken(refreshCtx, cred) refreshErr := auth.RefreshToken(refreshCtx, cred)
refreshCancel() refreshCancel()
if refreshErr != nil { if refreshErr != nil {
log.Printf("refresh failed: %v — initiating login", refreshErr) log.Warn().Err(refreshErr).Msg("refresh failed, initiating login")
cred = nil // fall through to login cred = nil // fall through to login
} else { } else {
log.Printf("token refreshed successfully") log.Info().Msg("token refreshed")
} }
} }
} }
@@ -54,14 +62,14 @@ func run() error {
if statErr == nil && (fi.Mode()&os.ModeCharDevice) == 0 { if statErr == nil && (fi.Mode()&os.ModeCharDevice) == 0 {
return fmt.Errorf("no valid credentials found; run the proxy interactively for initial login") return fmt.Errorf("no valid credentials found; run the proxy interactively for initial login")
} }
log.Printf("no valid credentials found, starting OAuth login flow...") log.Info().Msg("no credentials found, starting OAuth login")
cred, err = auth.Login(context.Background()) cred, err = auth.Login(context.Background())
if err != nil { if err != nil {
return fmt.Errorf("login failed: %w", err) return fmt.Errorf("login failed: %w", err)
} }
} }
log.Printf("loaded credential for %s", cred.Email) log.Info().Str("credential", cred.Email).Msg("credential loaded")
pool := auth.NewPool([]*auth.Credential{cred}) pool := auth.NewPool([]*auth.Credential{cred})
@@ -73,14 +81,14 @@ func run() error {
var profile *proxy.SniffedProfile var profile *proxy.SniffedProfile
if cfg.ClaudeBinary != "" { if cfg.ClaudeBinary != "" {
log.Printf("sniffing claude-code at %s...", cfg.ClaudeBinary) log.Info().Str("binary", cfg.ClaudeBinary).Msg("sniffing claude-code")
profile, err = proxy.SniffClaudeCode(cfg.ClaudeBinary) profile, err = proxy.SniffClaudeCode(cfg.ClaudeBinary)
if err != nil { if err != nil {
log.Printf("warning: sniff failed, using defaults: %v", err) log.Warn().Err(err).Msg("sniff failed, using defaults")
} }
} }
log.Printf("starting server on port %d", cfg.Port) log.Info().Int("port", cfg.Port).Msg("starting server")
srv := server.New(cfg, pool, profile) srv := server.New(cfg, pool, profile)
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
@@ -88,14 +96,14 @@ func run() error {
go func() { go func() {
<-quit <-quit
log.Printf("shutting down...") log.Info().Msg("shutting down")
cancel() cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel() defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil { if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("shutdown error: %v", err) log.Error().Err(err).Msg("shutdown error")
} }
}() }()
@@ -108,7 +116,7 @@ func run() error {
func main() { func main() {
if err := run(); err != nil { if err := run(); err != nil {
log.Printf("error: %v", err) log.Error().Err(err).Msg("fatal error")
os.Exit(1) os.Exit(1)
} }
} }