slog: use the slog.LevelVar unmarshal to parse log level flag

This commit is contained in:
HeshamTB 2024-03-13 00:03:14 +03:00
parent a9ad981137
commit defd32b3c8

View File

@ -102,18 +102,13 @@ func createCliApp() *cli.App {
Value: "INFO", Value: "INFO",
EnvVars: []string{"LOG_LEVEL"}, EnvVars: []string{"LOG_LEVEL"},
Action: func(ctx *cli.Context, s string) error { Action: func(ctx *cli.Context, s string) error {
switch s { lvl := new(slog.LevelVar)
case "INFO": err := lvl.UnmarshalText([]byte(s))
slog.SetLogLoggerLevel(slog.LevelInfo) if err != nil {
case "DEBUG": slog.Debug("Error on unmarshal log level flag")
slog.SetLogLoggerLevel(slog.LevelDebug) return err
case "WARN":
slog.SetLogLoggerLevel(slog.LevelWarn)
case "ERROR":
slog.SetLogLoggerLevel(slog.LevelError)
default:
return cli.Exit(fmt.Sprintf("Undefined log level: %s", s), 1)
} }
slog.SetLogLoggerLevel(lvl.Level())
return nil return nil
}, },
} }