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