diff --git a/configuration/config.go b/configuration/config.go index 688d37a490ff2a3df2d52fe3749459e904622826..cde4a2b5cb98912c77c7c4ad72f692abddecb38d 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "log/slog" "unicode" fp "path/filepath" @@ -58,14 +59,8 @@ func New() ( *Config, error ){ if cfg.Environment == "development" { cfg.LogLevel = "debug" } - possibleLogLevels := map[ string ] bool { - "error": true, - "debug": true, - } - if _, ok := possibleLogLevels[ cfg.LogLevel ]; !ok { - return nil, errors.New( - fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ), - ) + if _, err := cfg.GetLogLevel(); err != nil { + return nil, err } if len( cfg.DatabaseHost ) >= 1 && len( cfg.DatabasePassword ) >= 2 { @@ -106,3 +101,19 @@ func New() ( *Config, error ){ return &cfg, nil } + + +func ( cfg *Config ) GetLogLevel() ( slog.Level, error ){ + possibleLogLevels := map[ string ] slog.Level { + "error": slog.LevelError, + "debug": slog.LevelDebug, + } + level, ok := possibleLogLevels[ cfg.LogLevel ] + if !ok { + return slog.LevelError, errors.New( + fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ), + ) + }else{ + return level, nil + } +} \ No newline at end of file diff --git a/main.go b/main.go index 3beb4aee8ef124d1c2e4e17f0b0aea2dbece2ee5..3ba1aeebf78f7467926e0856d8b236200b839863 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "log/slog" "os" "os/signal" "syscall" @@ -20,9 +21,22 @@ import ( func main() { config, err := configuration.New() if err != nil { - log.Fatalf( "HTTP server failed to start: %v", err ) + slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) ) + os.Exit( 1 ) } + level, _ := config.GetLogLevel() + slog.SetDefault( + slog.New( + slog.NewTextHandler( + os.Stdout, + &slog.HandlerOptions{ + Level: level, + }, + ), + ), + ) + server := fiber.New( fiber.Config{ AppName: "webservice", DisableStartupMessage: config.Environment != "development", @@ -40,13 +54,15 @@ func main() { err = routing.SetRoutes( server, config, store, &isHealthy ) if err != nil { - log.Fatalf( "HTTP server failed to start: %v", err ) + slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) ) + os.Exit( 1 ) } go func(){ err := server.Listen( fmt.Sprintf( "%s:%d", config.Host, config.Port ) ) if err != nil { - log.Fatalf( "HTTP server failed to start: %v", err ) + slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) ) + os.Exit( 1 ) } }() diff --git a/routing/routes.go b/routing/routes.go index f1439c40275f0af90c9457d97eb7cdff1229af93..f1ddb4bad0b7778d8393203215f4035a0a6b2a0c 100644 --- a/routing/routes.go +++ b/routing/routes.go @@ -7,7 +7,7 @@ import ( "strings" "net/http" "html/template" - "log" + log "log/slog" "bytes" "mime" @@ -32,11 +32,13 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, if config.LogLevel == "debug" { router.All( "*", func( c *f.Ctx ) error { - log.Printf( "%s %s mime:%s agent:%s", - c.Method(), - c.Path(), - c.Get( f.HeaderContentType, c.Get( f.HeaderAccept, "" ) ), - c.Get( f.HeaderUserAgent ), + log.Debug( + fmt.Sprintf( "%s %s mime:%s agent:%s", + c.Method(), + c.Path(), + c.Get( f.HeaderContentType, c.Get( f.HeaderAccept, "" ) ), + c.Get( f.HeaderUserAgent ), + ), ) return c.Next() }) @@ -106,6 +108,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, } else { names, err := store.List() if err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) } @@ -135,6 +138,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, for _, envVar := range os.Environ() { _, err := c.WriteString( fmt.Sprintln( envVar ) ) if err != nil { + log.Debug( err.Error() ) c.Status( http.StatusInternalServerError ) return err } @@ -152,6 +156,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, name := strings.Clone( c.Params( "name" ) ) existingItem, err := store.Fetch( name ) if err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) } @@ -167,6 +172,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, statePathGroup.Get( "/:name", func( c *f.Ctx ) error { existingItem, err := store.Fetch( c.Params( "name" ) ) if err != nil { + log.Debug( err.Error() ) c.Status( http.StatusInternalServerError ) return c.Send( nil ) } @@ -193,6 +199,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, name := strings.Clone( c.Params( "name" ) ) existingItem, err := store.Fetch( name ) if err != nil { + log.Debug( err.Error() ) c.Status( http.StatusInternalServerError ) return c.Send( nil ) } @@ -216,6 +223,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, ) if err = store.Add( newItem ); err != nil { + log.Debug( err.Error() ) c.Status( http.StatusInternalServerError ) return c.Send( nil ) } @@ -229,6 +237,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, name := strings.Clone( c.Params( "name" ) ) existingItem, err := store.Fetch( name ) if err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) } @@ -237,6 +246,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, } if err = store.Remove( name ); err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) } @@ -248,6 +258,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, name := strings.Clone( c.Params( "name" ) ) existingItem, err := store.Fetch( name ) if err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) } @@ -269,6 +280,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, router.Get( "/states", func( c *f.Ctx ) error { names, err := store.List() if err != nil { + log.Debug( err.Error() ) return c.SendStatus( http.StatusInternalServerError ) }