Replace all log.Println and fmt.Printf with Zap structured logging

This commit is contained in:
2026-05-21 23:24:55 +02:00
parent f0653489d6
commit 89c31c2856
7 changed files with 184 additions and 162 deletions
+9 -8
View File
@@ -2,14 +2,15 @@ package main
import (
"context"
"fmt"
"log"
"music-server/internal/db"
"music-server/internal/logging"
"music-server/internal/server"
"net/http"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
)
//
@@ -41,15 +42,15 @@ func main() {
// Run graceful shutdown in a separate goroutine
go gracefulShutdown(server, done)
log.Printf("Open http://localhost%s in the browser", server.Addr)
logging.GetLogger().Info("Server starting", zap.String("address", server.Addr))
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(fmt.Sprintf("http server error: %s", err))
logging.GetLogger().Fatal("HTTP server error", zap.String("error", err.Error()))
}
// Wait for the graceful shutdown to complete
<-done
log.Println("Graceful shutdown complete.")
logging.GetLogger().Info("Graceful shutdown complete")
}
func gracefulShutdown(apiServer *http.Server, done chan bool) {
@@ -60,7 +61,7 @@ func gracefulShutdown(apiServer *http.Server, done chan bool) {
// Listen for the interrupt signal.
<-ctx.Done()
log.Println("shutting down gracefully, press Ctrl+C again to force")
logging.GetLogger().Info("Shutting down gracefully, press Ctrl+C again to force")
db.CloseDb()
// The context is used to inform the server it has 5 seconds to finish
@@ -68,10 +69,10 @@ func gracefulShutdown(apiServer *http.Server, done chan bool) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := apiServer.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
logging.GetLogger().Error("Server forced to shutdown with error", zap.String("error", err.Error()))
}
log.Println("Server exiting")
logging.GetLogger().Info("Server exiting")
// Notify the main goroutine that the shutdown is complete
done <- true