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"
|
"github.com/maddalax/htmgo/framework/service"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"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() {
|
func (app *App) start() {
|
||||||
|
|
||||||
|
slog.SetLogLoggerLevel(GetLogLevel())
|
||||||
|
|
||||||
app.Router.Use(func(h http.Handler) http.Handler {
|
app.Router.Use(func(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
cc := &RequestContext{
|
cc := &RequestContext{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue