Skip to content

Commit

Permalink
new logger when no config file provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Nov 9, 2023
1 parent f85919c commit 6a77b36
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,23 @@ 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.
// reads `homeDir/config/config.yaml` into `a.Config`
return a.loadConfigFile(rootCmd.Context())
if err := a.loadConfigFile(rootCmd.Context()); err != nil {
return err
}
// Inside persistent pre-run because this takes effect after flags are parsed.
if a.log == nil {
logLevel := a.viper.GetString("log-level")
debugMode := a.viper.GetBool("debug")
if debugMode {
logLevel = "debug"
}
log, err := newRootLogger(a.viper.GetString("log-format"), logLevel)
if err != nil {
return err
}
a.log = log
}
return nil
}

rootCmd.PersistentPostRun = func(cmd *cobra.Command, _ []string) {
Expand All @@ -100,7 +116,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)
}
Expand Down

0 comments on commit 6a77b36

Please sign in to comment.