From 09ee2de72936ea0b0edc5d3360ce1c72803a817b Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 19 May 2024 15:42:36 +0200 Subject: log in logfmt via zap, with nicer console output in dev --- cmd/server/main.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'cmd/server') diff --git a/cmd/server/main.go b/cmd/server/main.go index bae215a..464c438 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,12 +2,11 @@ package main import ( "fmt" - "log" - "log/slog" "os" "os/signal" "sync" + "website/internal/log" "website/internal/server" "github.com/ardanlabs/conf/v3" @@ -20,12 +19,6 @@ var ( ) func main() { - if os.Getenv("DEBUG") != "" { - slog.SetLogLoggerLevel(slog.LevelDebug) - } - log.SetFlags(log.LstdFlags | log.Lmsgprefix) - log.SetPrefix("server: ") - runtimeConfig := server.Config{} help, err := conf.Parse("", &runtimeConfig) if err != nil { @@ -33,23 +26,24 @@ func main() { fmt.Println(help) os.Exit(1) } - log.Panicf("parsing runtime configuration: %v", err) + panic("parsing runtime configuration" + err.Error()) } + log.Configure(runtimeConfig.Production) c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt) sv, err := server.New(&runtimeConfig) if err != nil { - log.Fatalf("error setting up server: %v", err) + log.Fatal("error setting up server", "error", err) } wg := &sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() sig := <-c - log.Printf("signal captured: %v", sig) + log.Info("signal captured", "sig", sig) <-sv.Stop() - slog.Debug("server stopped") + log.Debug("server stopped") }() sErr := make(chan error) @@ -59,13 +53,13 @@ func main() { sErr <- sv.Start() }() if !runtimeConfig.InDevServer { - log.Printf("server listening on %s", sv.Addr) + log.Info("server listening", "address", sv.Addr) } err = <-sErr if err != nil { // Error starting or closing listener: - log.Fatalf("error: %v", err) + log.Fatal("error", "error", err) } wg.Wait() } -- cgit 1.4.1