refactor(log): replace stdlib log with the application JSON logger
Every log.Printf/Fatalf becomes a structured slog call through internal/log: request handlers use the request-scoped logger (applog.FromContext, carrying request_id), startup failures exit via a fatal() helper that still emits JSON, and the seedsample/dumpschema CLIs bootstrap the same JSON logger. Stale client_test expectations aligned with the refactored wrapper-call logging (message casing, result attr, ERROR level). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,11 +10,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
applog "geniusrun/backend/internal/log"
|
||||
"geniusrun/backend/internal/store"
|
||||
)
|
||||
|
||||
@@ -26,6 +27,7 @@ type schemaEntry struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
slog.SetDefault(applog.NewLogger("info", os.Stdout))
|
||||
tmpDir, err := os.MkdirTemp("", "geniusrun-dumpschema")
|
||||
must(err)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
@@ -91,11 +93,12 @@ func main() {
|
||||
|
||||
outPath := "../docs/DATABASE.md"
|
||||
must(os.WriteFile(outPath, []byte(b.String()), 0644))
|
||||
log.Printf("wrote %s", outPath)
|
||||
slog.Info("wrote schema doc", "path", outPath)
|
||||
}
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
slog.Error("dumpschema", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -17,30 +16,42 @@ import (
|
||||
"geniusrun/backend/internal/auth"
|
||||
"geniusrun/backend/internal/config"
|
||||
"geniusrun/backend/internal/garmin"
|
||||
"geniusrun/backend/internal/log"
|
||||
applog "geniusrun/backend/internal/log"
|
||||
"geniusrun/backend/internal/store"
|
||||
)
|
||||
|
||||
// fatal logs through the application JSON logger and exits -- the
|
||||
// structured replacement for log.Fatalf, so even startup failures come
|
||||
// out as JSON lines.
|
||||
func fatal(msg string, err error) {
|
||||
slog.Error(msg, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Bootstrap logger at info so failures loading the env config itself
|
||||
// (which carries the real log level) are still emitted as JSON.
|
||||
slog.SetDefault(applog.NewLogger("info", os.Stdout))
|
||||
|
||||
envCfg, err := config.LoadEnv()
|
||||
if err != nil {
|
||||
log.Fatalf("config: %v", err)
|
||||
fatal("load env config", err)
|
||||
}
|
||||
slog.SetDefault(applog.NewLogger(envCfg.LogLevel, os.Stdout))
|
||||
|
||||
db, err := store.Open(envCfg.DBPath)
|
||||
if err != nil {
|
||||
log.Fatalf("open database: %v", err)
|
||||
fatal("open database", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
overrides, err := db.ConfigValues(context.Background())
|
||||
if err != nil {
|
||||
log.Fatalf("app config: %v", err)
|
||||
fatal("read app config overrides", err)
|
||||
}
|
||||
appCfg, err := config.LoadApp(overrides)
|
||||
if err != nil {
|
||||
log.Fatalf("app config: %v", err)
|
||||
fatal("load app config", err)
|
||||
}
|
||||
|
||||
authVerifier, err := auth.NewOIDCVerifier(context.Background(), auth.OIDCConfig{
|
||||
@@ -51,7 +62,7 @@ func main() {
|
||||
RequiredRole: envCfg.OIDCRequiredRole,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("oidc: %v", err)
|
||||
fatal("oidc verifier setup", err)
|
||||
}
|
||||
|
||||
server := api.NewServer(
|
||||
@@ -80,17 +91,17 @@ func main() {
|
||||
|
||||
httpServer := &http.Server{Addr: envCfg.BackendAddr, Handler: server.Router()}
|
||||
go func() {
|
||||
log.Printf("geniusrund listening on %s", envCfg.BackendAddr)
|
||||
slog.Info("geniusrund listening", "addr", envCfg.BackendAddr)
|
||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("http server: %v", err)
|
||||
fatal("http server", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Println("shutting down...")
|
||||
slog.Info("shutting down")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||
log.Printf("http server shutdown: %v", err)
|
||||
slog.Error("http server shutdown", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,22 +9,25 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"geniusrun/backend/internal/classify"
|
||||
"geniusrun/backend/internal/garmin"
|
||||
applog "geniusrun/backend/internal/log"
|
||||
"geniusrun/backend/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
slog.SetDefault(applog.NewLogger("info", os.Stdout))
|
||||
dbPath := flag.String("db", "geniusrun_sample.db", "path to the SQLite database to seed")
|
||||
flag.Parse()
|
||||
|
||||
ctx := context.Background()
|
||||
db, err := store.Open(*dbPath)
|
||||
if err != nil {
|
||||
log.Fatalf("open db: %v", err)
|
||||
fatal("open db", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
@@ -233,10 +236,17 @@ func seedIntervalLapsAndSamples(ctx context.Context, db *store.DB, userID, activ
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fatal("seedsample", err)
|
||||
}
|
||||
}
|
||||
|
||||
// fatal logs through the application JSON logger and exits -- the
|
||||
// structured replacement for log.Fatal.
|
||||
func fatal(msg string, err error) {
|
||||
slog.Error(msg, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func mustFindKindID(ctx context.Context, db *store.DB, userID int64, name string) int64 {
|
||||
kinds, err := db.ListWorkoutKinds(ctx, userID, false)
|
||||
must(err)
|
||||
@@ -245,6 +255,7 @@ func mustFindKindID(ctx context.Context, db *store.DB, userID int64, name string
|
||||
return k.ID
|
||||
}
|
||||
}
|
||||
log.Fatalf("seedsample: no workout kind named %q found for the seeded user (did ProvisionUser seed the taxonomy?)", name)
|
||||
slog.Error("no workout kind with that name for the seeded user (did ProvisionUser seed the taxonomy?)", "kind", name)
|
||||
os.Exit(1)
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user