From 07b17b3c82b115646bea27a240b48f443f9cce57 Mon Sep 17 00:00:00 2001 From: maddalax Date: Sat, 28 Sep 2024 12:51:23 -0500 Subject: [PATCH] set log level --- framework/h/app.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/framework/h/app.go b/framework/h/app.go index 95b5698..1780a76 100644 --- a/framework/h/app.go +++ b/framework/h/app.go @@ -8,8 +8,10 @@ import ( "github.com/maddalax/htmgo/framework/service" "log/slog" "net/http" + "os" "os/exec" "runtime" + "strings" "time" ) @@ -91,8 +93,28 @@ func (app *App) UseWithContext(h func(w http.ResponseWriter, r *http.Request, co }) } +func GetLogLevel() slog.Level { + // Get the log level from the environment variable + logLevel := os.Getenv("LOG_LEVEL") + switch strings.ToUpper(logLevel) { + case "DEBUG": + return slog.LevelDebug + case "INFO": + return slog.LevelInfo + case "WARN": + return slog.LevelWarn + case "ERROR": + return slog.LevelError + default: + // Default to INFO if no valid log level is set + return slog.LevelInfo + } +} + func (app *App) start() { + slog.SetLogLoggerLevel(GetLogLevel()) + app.Router.Use(func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { cc := &RequestContext{