Skip to content

Commit 2c60f6c

Browse files
authored
feat: add log-level in global config (cosmos#1324)
* 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
1 parent 9b80dd0 commit 2c60f6c

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [\#1231](https://github.com/cosmos/relayer/pull/1231) Reduce get bech32 prefix when get signer.
2626
* [\#1302](https://github.com/cosmos/relayer/pull/1302) Avoid packet get relayed when estimated gas is higher than max gas.
2727
* [\#1303](https://github.com/cosmos/relayer/pull/1303) Add missing max gas amount on txf to avoid estimate less gas when simualte runTx.
28+
* [\#1324](https://github.com/cosmos/relayer/pull/1324) Add log-level in global config.
2829
* [\#1325](https://github.com/cosmos/relayer/pull/1325) Ignore only file not exist error when loadConfigFile.
2930

3031
## v0.9.3

cmd/appstate.go

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ type appState struct {
3131
config *Config
3232
}
3333

34+
func (a *appState) initLogger(configLogLevel string) error {
35+
logLevel := a.viper.GetString("log-level")
36+
if a.viper.GetBool("debug") {
37+
logLevel = "debug"
38+
} else if logLevel == "" {
39+
logLevel = configLogLevel
40+
}
41+
log, err := newRootLogger(a.viper.GetString("log-format"), logLevel)
42+
if err != nil {
43+
return err
44+
}
45+
46+
a.log = log
47+
return nil
48+
}
49+
3450
func (a *appState) configPath() string {
3551
return path.Join(a.homePath, "config", "config.yaml")
3652
}
@@ -60,6 +76,10 @@ func (a *appState) loadConfigFile(ctx context.Context) error {
6076
return fmt.Errorf("error unmarshalling config: %w", err)
6177
}
6278

79+
if a.log == nil {
80+
a.initLogger(cfgWrapper.Global.LogLevel)
81+
}
82+
6383
// retrieve the runtime configuration from the disk configuration.
6484
newCfg, err := cfgWrapper.RuntimeConfig(ctx, a)
6585
if err != nil {

cmd/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ type GlobalConfig struct {
492492
Timeout string `yaml:"timeout" json:"timeout"`
493493
Memo string `yaml:"memo" json:"memo"`
494494
LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"`
495+
LogLevel string `yaml:"log-level" json:"log-level"`
495496
}
496497

497498
// newDefaultGlobalConfig returns a global config with defaults set
@@ -501,6 +502,7 @@ func newDefaultGlobalConfig(memo string) GlobalConfig {
501502
Timeout: "10s",
502503
LightCacheSize: 20,
503504
Memo: memo,
505+
LogLevel: "info",
504506
}
505507
}
506508

cmd/root.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,15 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
7373

7474
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
7575
// Inside persistent pre-run because this takes effect after flags are parsed.
76-
if log == nil {
77-
log, err := newRootLogger(a.viper.GetString("log-format"), a.viper.GetBool("debug"))
78-
if err != nil {
79-
return err
80-
}
81-
82-
a.log = log
83-
}
84-
8576
// reads `homeDir/config/config.yaml` into `a.Config`
86-
return a.loadConfigFile(rootCmd.Context())
77+
if err := a.loadConfigFile(rootCmd.Context()); err != nil {
78+
return err
79+
}
80+
// Inside persistent pre-run because this takes effect after flags are parsed.
81+
if a.log == nil {
82+
a.initLogger("")
83+
}
84+
return nil
8785
}
8886

8987
rootCmd.PersistentPostRun = func(cmd *cobra.Command, _ []string) {
@@ -108,6 +106,12 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
108106
panic(err)
109107
}
110108

109+
// Register --log-level flag
110+
rootCmd.PersistentFlags().String("log-level", "", "log level format (info, debug, warn, error, panic or fatal)")
111+
if err := a.viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil {
112+
panic(err)
113+
}
114+
111115
// Register subcommands
112116
rootCmd.AddCommand(
113117
configCmd(a),
@@ -171,7 +175,7 @@ func Execute() {
171175
}
172176
}
173177

174-
func newRootLogger(format string, debug bool) (*zap.Logger, error) {
178+
func newRootLogger(format string, logLevel string) (*zap.Logger, error) {
175179
config := zap.NewProductionEncoderConfig()
176180
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
177181
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) {
191195
}
192196

193197
level := zap.InfoLevel
194-
if debug {
198+
switch logLevel {
199+
case "debug":
195200
level = zap.DebugLevel
201+
case "warn":
202+
level = zapcore.WarnLevel
203+
case "error":
204+
level = zapcore.ErrorLevel
205+
case "panic":
206+
level = zapcore.PanicLevel
207+
case "fatal":
208+
level = zapcore.FatalLevel
196209
}
197210
return zap.New(zapcore.NewCore(
198211
enc,

examples/config_EXAMPLE.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ global:
33
timeout: 10s
44
memo: ""
55
light-cache-size: 20
6+
log-level: "info"
67
chains:
78
cosmoshub:
89
type: cosmos

0 commit comments

Comments
 (0)