diff --git a/cmd/appstate.go b/cmd/appstate.go index 1675aaad56..fa4a5767af 100644 --- a/cmd/appstate.go +++ b/cmd/appstate.go @@ -55,16 +55,11 @@ func (a *appState) loadConfigFile(ctx context.Context) error { if err != nil { return fmt.Errorf("error unmarshalling config: %w", err) } - if a.log == nil { - debugMode := a.viper.GetBool("debug") - logLevel := "" - if debugMode { - logLevel = "debug" - } else { - logLevel = a.viper.GetString("log-level") - if logLevel == "" { - logLevel = cfgWrapper.Global.LogLevel - } + debugMode := a.viper.GetBool("debug") + if !debugMode { + logLevel := a.viper.GetString("log-level") + if logLevel == "" { + logLevel = cfgWrapper.Global.LogLevel } log, err := newRootLogger(a.viper.GetString("log-format"), logLevel) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 3338ca6dcf..672f931e5c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -73,6 +73,17 @@ func NewRootCmd(log *zap.Logger) *cobra.Command { rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { // Inside persistent pre-run because this takes effect after flags are parsed. + if log == nil { + debugMode := a.viper.GetBool("debug") + if !debugMode { + logLevel := a.viper.GetString("log-level") + log, err := newRootLogger(a.viper.GetString("log-format"), logLevel) + if err != nil { + return err + } + a.log = log + } + } // reads `homeDir/config/config.yaml` into `a.Config` return a.loadConfigFile(rootCmd.Context()) } @@ -100,7 +111,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command { } // Register --log-level flag - rootCmd.PersistentFlags().String("log-level", "info", "log level format (info, debug, warn, error, panic or fatal)") + rootCmd.PersistentFlags().String("log-level", "", "log level format (info, debug, warn, error, panic or fatal)") if err := a.viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil { panic(err) }