about summary refs log tree commit diff stats
path: root/cmd/server
diff options
context:
space:
mode:
authorAlan Pearce2024-05-19 15:42:36 +0200
committerAlan Pearce2024-05-19 15:53:32 +0200
commit09ee2de72936ea0b0edc5d3360ce1c72803a817b (patch)
tree427a590fba06e58554586aad8995fcb6e7a559f3 /cmd/server
parent4064f933a965f2fb2ce43d3c2d5bd54ccd33978c (diff)
downloadwebsite-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.lz
website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.zst
website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.zip
log in logfmt via zap, with nicer console output in dev
Diffstat (limited to 'cmd/server')
-rw-r--r--cmd/server/main.go22
1 files changed, 8 insertions, 14 deletions
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()
 }