Skip to content

Commit

Permalink
feat: add log-level in global config (#1324)
Browse files Browse the repository at this point in the history
* feat: add log-level in global config

* allow config logger get overwritten

* register log-level flag

* allow flag overwrite config

* allow debug flag overwrite log level

* new logger when no config file provided

* reuse with initLogger
  • Loading branch information
mmsqe authored Nov 10, 2023
1 parent 9b80dd0 commit 2c60f6c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -501,6 +502,7 @@ func newDefaultGlobalConfig(memo string) GlobalConfig {
Timeout: "10s",
LightCacheSize: 20,
Memo: memo,
LogLevel: "info",
}
}

Expand Down
37 changes: 25 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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"))
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions examples/config_EXAMPLE.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ global:
timeout: 10s
memo: ""
light-cache-size: 20
log-level: "info"
chains:
cosmoshub:
type: cosmos
Expand Down

0 comments on commit 2c60f6c

Please sign in to comment.