@@ -12,16 +12,17 @@ package main
1212import (
1313 "context"
1414 "fmt"
15- "log"
1615 "net/http"
1716 "os"
1817 "os/signal"
1918 "syscall"
2019 "time"
2120
2221 "github.com/commitdev/zero-notification-service/internal/config"
22+ "github.com/commitdev/zero-notification-service/internal/log"
2323 "github.com/commitdev/zero-notification-service/internal/server"
2424 "github.com/commitdev/zero-notification-service/internal/service"
25+ "go.uber.org/zap"
2526)
2627
2728var (
@@ -30,13 +31,16 @@ var (
3031)
3132
3233func main () {
33- fmt .Printf ("zero-notification-service version: %v, build: %v \n " , appVersion , appBuild )
34+ config := config .GetConfig ()
35+
36+ log .Init (config )
37+ defer zap .S ().Sync () // Flush logs when the process ends
38+
39+ zap .S ().Infow ("zero-notification-service" , "version" , appVersion , "build" , appBuild )
3440
3541 // Heartbeat for liveness check
3642 go heartbeat ()
3743
38- config := config .GetConfig ()
39-
4044 EmailApiService := service .NewEmailApiService (config )
4145 EmailApiController := server .NewEmailApiController (EmailApiService )
4246
@@ -57,34 +61,34 @@ func main() {
5761
5862 // Run the server in a goroutine
5963 go func () {
60- log . Printf ("Serving at http://%s/" , serverAddress )
64+ zap . S (). Infof ("Serving at http://%s/" , serverAddress )
6165 err := server .ListenAndServe ()
6266 if err != http .ErrServerClosed {
63- log .Fatalf ("Fatal error while serving HTTP: %v\n " , err )
67+ zap . S () .Fatalf ("Fatal error while serving HTTP: %v\n " , err )
6468 close (stop )
6569 }
6670 }()
6771
6872 // Block while reading from the channel until we receive a signal
6973 sig := <- stop
70- log . Printf ("Received signal %s, starting graceful shutdown" , sig )
74+ zap . S (). Infof ("Received signal %s, starting graceful shutdown" , sig )
7175
7276 // Give connections some time to drain
7377 ctx , cancel := context .WithTimeout (context .Background (), config .GracefulShutdownTimeout * time .Second )
7478 defer cancel ()
7579 err := server .Shutdown (ctx )
7680 if err != nil {
77- log .Fatalf ("Error during shutdown, client requests have been terminated: %v\n " , err )
81+ zap . S () .Fatalf ("Error during shutdown, client requests have been terminated: %v\n " , err )
7882 } else {
79- log . Println ("Graceful shutdown complete" )
83+ zap . S (). Infof ("Graceful shutdown complete" )
8084 }
8185}
8286
8387func heartbeat () {
8488 for range time .Tick (4 * time .Second ) {
8589 fh , err := os .Create ("/tmp/service-alive" )
8690 if err != nil {
87- log . Println ("Unable to write file for liveness check!" )
91+ zap . S (). Warnf ("Unable to write file for liveness check!" )
8892 } else {
8993 fh .Close ()
9094 }
0 commit comments