diff --git a/core/om/root.go b/core/om/root.go index 179829711..0ddd23677 100644 --- a/core/om/root.go +++ b/core/om/root.go @@ -3,7 +3,6 @@ package om import ( // Necessary to use go:embed _ "embed" - "fmt" "os" "path/filepath" @@ -189,7 +188,8 @@ func init() { root.PersistentFlags().StringVar(&configFlag, "config", "", "Config file (default \"$HOME/.opensvc.yaml\").") root.PersistentFlags().StringVar(&colorFlag, "color", "auto", "Output colorization yes|no|auto.") root.PersistentFlags().StringVar(&serverFlag, "server", "", "URI of the opensvc api server.") - root.PersistentFlags().StringVar(&logFlag, "log", "", "Display logs on the console at the specified level.") + root.PersistentFlags().StringVar(&logFlag, "log", "info", "Display logs on the console with one of the specified level: "+ + "trace, debug, info, warn, error, fatal, panic, none") root.PersistentFlags().BoolVar(&callerFlag, "caller", false, "Show caller : in logs.") } diff --git a/util/logging/logging.go b/util/logging/logging.go index afc0e3b98..695d3581c 100644 --- a/util/logging/logging.go +++ b/util/logging/logging.go @@ -94,14 +94,20 @@ func SetDefaultConsoleWriter(w zerolog.ConsoleWriter) { consoleWriter = w } -func Logger() zerolog.Logger { - return log.Logger -} - // Configure sets up the logging framework func Configure(config Config) error { var writers []io.Writer + if config.Level == "none" { + zerolog.SetGlobalLevel(zerolog.NoLevel) + } else if config.Level == "" { + zerolog.SetGlobalLevel(zerolog.InfoLevel) + } else if configLevel, err := zerolog.ParseLevel(config.Level); err != nil { + return fmt.Errorf("invalid log level %s", config.Level) + } else { + zerolog.SetGlobalLevel(configLevel) + } + zerolog.TimeFieldFormat = time.RFC3339Nano if writer := journald.NewJournalDWriter(); writer != nil { @@ -122,23 +128,6 @@ func Configure(config Config) error { logger := log.Output(mw) - switch config.Level { - case "debug": - logger = logger.Level(zerolog.DebugLevel) - case "info": - logger = logger.Level(zerolog.InfoLevel) - case "warn", "warning": - logger = logger.Level(zerolog.WarnLevel) - case "error": - logger = logger.Level(zerolog.ErrorLevel) - case "fatal": - logger = logger.Level(zerolog.FatalLevel) - case "panic": - logger = logger.Level(zerolog.PanicLevel) - default: - logger = logger.Level(zerolog.InfoLevel) - } - if config.WithCaller { // skip one more for plog wrappers logger = logger.With().CallerWithSkipFrameCount(zerolog.CallerSkipFrameCount + 1).Logger()