Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Logger not correctly applying named config #696

Merged
merged 4 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func parseAndConfigLogAllParams(ctx context.Context, cfg *config.LoggingConfig,
return fmt.Errorf("couldn't parse kv bool: %w", err)
}
logcfg.NoColor = boolValue
case "caller": // bool
boolValue, err := strconv.ParseBool(parsedKV[1])
if err != nil {
return fmt.Errorf("couldn't parse kv bool: %w", err)
}
logcfg.Caller = boolValue
default:
return fmt.Errorf("unknown parameter for logger: %s", param)
}
Expand Down
8 changes: 4 additions & 4 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func init() {
"loglevel", cfg.Log.Level,
"Log level to use. Options are debug, info, error, fatal",
)
err := viper.BindPFlag("logging.level", rootCmd.PersistentFlags().Lookup("loglevel"))
err := viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("loglevel"))
if err != nil {
log.FatalE(context.Background(), "Could not bind logging.loglevel", err)
}
Expand All @@ -94,7 +94,7 @@ func init() {
"logoutput", cfg.Log.OutputPath,
"Log output path",
)
err = viper.BindPFlag("logging.outputpath", rootCmd.PersistentFlags().Lookup("logoutput"))
err = viper.BindPFlag("log.outputpath", rootCmd.PersistentFlags().Lookup("logoutput"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.outputpath", err)
}
Expand All @@ -103,7 +103,7 @@ func init() {
"logformat", cfg.Log.Format,
"Log format to use. Options are csv, json",
)
err = viper.BindPFlag("logging.format", rootCmd.PersistentFlags().Lookup("logformat"))
err = viper.BindPFlag("log.format", rootCmd.PersistentFlags().Lookup("logformat"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.format", err)
}
Expand All @@ -112,7 +112,7 @@ func init() {
"logtrace", cfg.Log.Stacktrace,
"Include stacktrace in error and fatal logs",
)
err = viper.BindPFlag("logging.stacktrace", rootCmd.PersistentFlags().Lookup("logtrace"))
err = viper.BindPFlag("log.stacktrace", rootCmd.PersistentFlags().Lookup("logtrace"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.stacktrace", err)
}
Expand Down
6 changes: 1 addition & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,6 @@ func defaultLogConfig() *LoggingConfig {
}

func (logcfg *LoggingConfig) validate() error {
switch logcfg.Level {
case logLevelDebug, logLevelInfo, logLevelError, logLevelFatal:
default:
return fmt.Errorf("invalid log level: %s", logcfg.Level)
}
return nil
}

Expand Down Expand Up @@ -408,6 +403,7 @@ func (logcfg LoggingConfig) ToLoggerConfig() (logging.Config, error) {
DisableColor: logging.NewDisableColorOption(logcfg.NoColor),
EncoderFormat: logging.NewEncoderFormatOption(encfmt),
OutputPaths: []string{logcfg.OutputPath},
EnableCaller: logging.NewEnableCallerOption(logcfg.Caller),
OverridesByLoggerName: overrides,
}, nil
}
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/cli/log_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestCLILogsToStderrGivenNamedLogLevel(t *testing.T) {
log1.Error(ctx, "error")
log1.Debug(ctx, "debug")
log2.Info(ctx, "info")
log3.Debug(ctx, "info")
log3.Debug(ctx, "debug") // wont print, as logger3 will use global level defined above as 'error'
log3.Info(ctx, "info") // wont print, as logger3 will use global level defined above as 'error'
},
)

Expand Down