From bc312cca048ac5fb9b35d08f5b898fab417cd560 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 8 Nov 2023 21:31:33 +0800 Subject: [PATCH] feat: add log-level in global config --- CHANGELOG.md | 1 + cmd/appstate.go | 5 +++++ cmd/config.go | 2 ++ cmd/root.go | 22 +++++++++++----------- examples/config_EXAMPLE.yaml | 1 + 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5a46901d..68a2628ae 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. ## v0.9.3 diff --git a/cmd/appstate.go b/cmd/appstate.go index 738e9af40..9364ab42e 100644 --- a/cmd/appstate.go +++ b/cmd/appstate.go @@ -55,6 +55,11 @@ func (a *appState) loadConfigFile(ctx context.Context) error { if err != nil { return fmt.Errorf("error unmarshalling config: %w", err) } + log, err := newRootLogger(a.viper.GetString("log-format"), cfgWrapper.Global.LogLevel) + if err != nil { + return err + } + a.log = log // retrieve the runtime configuration from the disk configuration. newCfg, err := cfgWrapper.RuntimeConfig(ctx, a) 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..83c88105e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -73,15 +73,6 @@ 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()) } @@ -171,7 +162,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 +182,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