diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd3bce1f..e5bac4406 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * [\#1231](https://github.com/cosmos/relayer/pull/1231) Reduce get bech32 prefix when get signer. * [\#1302](https://github.com/cosmos/relayer/pull/1302) Avoid packet get relayed when estimated gas is higher than max gas. * [\#1303](https://github.com/cosmos/relayer/pull/1303) Add missing max gas amount on txf to avoid estimate less gas when simualte runTx. +* [\#1324](https://github.com/cosmos/relayer/pull/1324) Add log-level in global config. * [\#1325](https://github.com/cosmos/relayer/pull/1325) Ignore only file not exist error when loadConfigFile. ## v0.9.3 diff --git a/cmd/appstate.go b/cmd/appstate.go index 887be2cca..44aff4736 100644 --- a/cmd/appstate.go +++ b/cmd/appstate.go @@ -31,6 +31,22 @@ type appState struct { config *Config } +func (a *appState) initLogger(configLogLevel string) error { + logLevel := a.viper.GetString("log-level") + if a.viper.GetBool("debug") { + logLevel = "debug" + } else if logLevel == "" { + logLevel = configLogLevel + } + log, err := newRootLogger(a.viper.GetString("log-format"), logLevel) + if err != nil { + return err + } + + a.log = log + return nil +} + func (a *appState) configPath() string { return path.Join(a.homePath, "config", "config.yaml") } @@ -60,6 +76,10 @@ func (a *appState) loadConfigFile(ctx context.Context) error { return fmt.Errorf("error unmarshalling config: %w", err) } + if a.log == nil { + a.initLogger(cfgWrapper.Global.LogLevel) + } + // retrieve the runtime configuration from the disk configuration. newCfg, err := cfgWrapper.RuntimeConfig(ctx, a) if err != nil { diff --git a/cmd/config.go b/cmd/config.go index 80ae95e4c..a6337ed94 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -492,6 +492,7 @@ type GlobalConfig struct { Timeout string `yaml:"timeout" json:"timeout"` Memo string `yaml:"memo" json:"memo"` LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"` + LogLevel string `yaml:"log-level" json:"log-level"` } // newDefaultGlobalConfig returns a global config with defaults set @@ -501,6 +502,7 @@ func newDefaultGlobalConfig(memo string) GlobalConfig { Timeout: "10s", LightCacheSize: 20, Memo: memo, + LogLevel: "info", } } diff --git a/cmd/root.go b/cmd/root.go index 1a6cfc3d3..52e316055 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -73,17 +73,15 @@ 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 { - log, err := newRootLogger(a.viper.GetString("log-format"), a.viper.GetBool("debug")) - if err != nil { - return err - } - - a.log = log - } - // 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 { + a.initLogger("") + } + return nil } rootCmd.PersistentPostRun = func(cmd *cobra.Command, _ []string) { @@ -108,6 +106,12 @@ func NewRootCmd(log *zap.Logger) *cobra.Command { panic(err) } + // Register --log-level flag + 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) + } + // Register subcommands rootCmd.AddCommand( configCmd(a), @@ -171,7 +175,7 @@ func Execute() { } } -func newRootLogger(format string, debug bool) (*zap.Logger, error) { +func newRootLogger(format string, logLevel string) (*zap.Logger, error) { config := zap.NewProductionEncoderConfig() config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { encoder.AppendString(ts.UTC().Format("2006-01-02T15:04:05.000000Z07:00")) @@ -191,8 +195,17 @@ func newRootLogger(format string, debug bool) (*zap.Logger, error) { } level := zap.InfoLevel - if debug { + switch logLevel { + case "debug": level = zap.DebugLevel + case "warn": + level = zapcore.WarnLevel + case "error": + level = zapcore.ErrorLevel + case "panic": + level = zapcore.PanicLevel + case "fatal": + level = zapcore.FatalLevel } return zap.New(zapcore.NewCore( enc, diff --git a/examples/config_EXAMPLE.yaml b/examples/config_EXAMPLE.yaml index dd4ad343d..900d9d608 100644 --- a/examples/config_EXAMPLE.yaml +++ b/examples/config_EXAMPLE.yaml @@ -3,6 +3,7 @@ global: timeout: 10s memo: "" light-cache-size: 20 + log-level: "info" chains: cosmoshub: type: cosmos