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:
@@ -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