From defd32b3c88f1466ca414b37873c6fb7e248657c Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 13 Mar 2024 00:03:14 +0300 Subject: [PATCH] slog: use the slog.LevelVar unmarshal to parse log level flag --- cmd/hvpn-node/hvpn-node.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/cmd/hvpn-node/hvpn-node.go b/cmd/hvpn-node/hvpn-node.go index c452528..ec2aacb 100644 --- a/cmd/hvpn-node/hvpn-node.go +++ b/cmd/hvpn-node/hvpn-node.go @@ -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 }, }