@@ -3,13 +3,15 @@ package main
33import (
44 "context"
55 "fmt"
6- "log"
6+ "log/slog "
77 "net/http"
8+ "os"
89 "os/signal"
910 "syscall"
1011 "time"
1112
1213 "github.com/quyxishi/whitebox/internal/api"
14+ mlog "github.com/quyxishi/whitebox/internal/log"
1315)
1416
1517func gracefulShutdown (apiServer * http.Server , done chan bool ) {
@@ -20,24 +22,34 @@ func gracefulShutdown(apiServer *http.Server, done chan bool) {
2022 // Listen for the interrupt signal.
2123 <- ctx .Done ()
2224
23- log . Println ("Shutting down gracefully, press Ctrl+C again to force" )
25+ slog . Info ("Shutting down gracefully, press Ctrl+C again to force" )
2426 stop () // Allow Ctrl+C to force shutdown
2527
2628 // The context is used to inform the server it has 5 seconds to finish
2729 // the request it is currently handling
2830 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
2931 defer cancel ()
3032 if err := apiServer .Shutdown (ctx ); err != nil {
31- log . Printf ("Server forced to shutdown with error: %v" , err )
33+ slog . Error ("Server forced to shutdown" , "due" , err . Error () )
3234 }
3335
34- log . Println ("Server exiting" )
36+ slog . Info ("Server exiting" )
3537
3638 // Notify the main goroutine that the shutdown is complete
3739 done <- true
3840}
3941
4042func main () {
43+ opts := mlog.ModuleHandlerOptions {
44+ SlogOpts : slog.HandlerOptions {
45+ Level : slog .LevelDebug ,
46+ },
47+ }
48+ handler := mlog .NewModuleHandler (os .Stdout , & opts )
49+ slog .SetDefault (slog .New (handler ))
50+
51+ // *
52+
4153 server := api .NewServer ()
4254
4355 // Create a done channel to signal when the shutdown is complete
@@ -53,5 +65,5 @@ func main() {
5365
5466 // Wait for the graceful shutdown to complete
5567 <- done
56- log . Println ("Graceful shutdown complete." )
68+ slog . Info ("Graceful shutdown complete." )
5769}
0 commit comments