Skip to content
Snippets Groups Projects
Commit 8aba7144 authored by gjahn's avatar gjahn
Browse files

log-level based logging added to ease debugging persistence on K8s

* new slog in stdlib is utilized for that and replaces 'log' pkg
* configuration pkg is used validate and now set/define/refer
  log level values
* the 'error' level call is not fatal, thus os.Exit is called
  instead
parent 5a9f952d
No related branches found
No related tags found
No related merge requests found
Pipeline #51875 passed
......@@ -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
......@@ -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 )
}
}()
......
......@@ -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 )
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment