set log level
This commit is contained in:
parent
47ceddb1f1
commit
07b17b3c82
1 changed files with 22 additions and 0 deletions
|
|
@ -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{
|
||||
|
|
|
|||
Loading…
Reference in a new issue