Skip to content

Commit

Permalink
feat: add log-level in global config
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Nov 8, 2023
1 parent 26aa983 commit bc312cc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 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.

## v0.9.3

Expand Down
5 changes: 5 additions & 0 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
22 changes: 11 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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"))
Expand All @@ -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,
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 bc312cc

Please sign in to comment.