From 44556cb209c9a1845129608eda6a43f51f6410eb Mon Sep 17 00:00:00 2001 From: Orpheus Lummis Date: Tue, 19 Jul 2022 18:01:56 -0400 Subject: [PATCH] fix: Consider `--logoutput` CLI flag properly (#645) --- cli/root.go | 30 +++++++++++++++--------------- config/config.go | 30 +++++++++++++++--------------- config/config_test.go | 30 +++++++++++++++--------------- config/configfile.go | 12 ++++++------ 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/cli/root.go b/cli/root.go index 9f4c0843ca..04c4e94982 100644 --- a/cli/root.go +++ b/cli/root.go @@ -93,48 +93,48 @@ func init() { ) rootCmd.PersistentFlags().String( - "loglevel", cfg.Logging.Level, + "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) + log.FatalE(context.Background(), "Could not bind log.loglevel", err) } rootCmd.PersistentFlags().String( - "logoutput", cfg.Logging.OutputPath, + "logoutput", cfg.Log.OutputPath, "log output path", ) - err = viper.BindPFlag("logging.output", rootCmd.PersistentFlags().Lookup("logoutput")) + err = viper.BindPFlag("log.outputpath", rootCmd.PersistentFlags().Lookup("logoutput")) if err != nil { - log.FatalE(context.Background(), "Could not bind logging.output", err) + log.FatalE(context.Background(), "Could not bind log.outputpath", err) } rootCmd.PersistentFlags().String( - "logformat", cfg.Logging.Format, + "logformat", cfg.Log.Format, "log format to use. Options are text, 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 logging.format", err) + log.FatalE(context.Background(), "Could not bind log.format", err) } rootCmd.PersistentFlags().Bool( - "logtrace", cfg.Logging.Stacktrace, + "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 logging.stacktrace", err) + log.FatalE(context.Background(), "Could not bind log.stacktrace", err) } rootCmd.PersistentFlags().Bool( - "logcolor", cfg.Logging.Color, + "logcolor", cfg.Log.Color, "enable colored output", ) - err = viper.BindPFlag("logging.color", rootCmd.PersistentFlags().Lookup("logcolor")) + err = viper.BindPFlag("log.color", rootCmd.PersistentFlags().Lookup("logcolor")) if err != nil { - log.FatalE(context.Background(), "Could not bind logging.color", err) + log.FatalE(context.Background(), "Could not bind log.color", err) } rootCmd.PersistentFlags().String( diff --git a/config/config.go b/config/config.go index fd5c6942c7..b6afbe6b68 100644 --- a/config/config.go +++ b/config/config.go @@ -73,7 +73,7 @@ type Config struct { Datastore *DatastoreConfig API *APIConfig Net *NetConfig - Logging *LoggingConfig + Log *LogConfig } // Load Config and handles parameters from config file, environment variables. @@ -141,7 +141,7 @@ func DefaultConfig() *Config { Datastore: defaultDatastoreConfig(), API: defaultAPIConfig(), Net: defaultNetConfig(), - Logging: defaultLoggingConfig(), + Log: defaultLogConfig(), } } @@ -155,8 +155,8 @@ func (cfg *Config) validateBasic() error { if err := cfg.Net.validateBasic(); err != nil { return fmt.Errorf("failed to validate Net config: %w", err) } - if err := cfg.Logging.validateBasic(); err != nil { - return fmt.Errorf("failed to validate Logging config: %w", err) + if err := cfg.Log.validateBasic(); err != nil { + return fmt.Errorf("failed to validate Log config: %w", err) } return nil } @@ -325,8 +325,8 @@ func (cfg *Config) NodeConfig() node.NodeOpt { } } -// LoggingConfig configures output and logger. -type LoggingConfig struct { +// LogConfig configures output and logger. +type LogConfig struct { Level string Stacktrace bool Format string @@ -334,8 +334,8 @@ type LoggingConfig struct { Color bool } -func defaultLoggingConfig() *LoggingConfig { - return &LoggingConfig{ +func defaultLogConfig() *LogConfig { + return &LogConfig{ Level: "info", Stacktrace: false, Format: "csv", @@ -344,14 +344,14 @@ func defaultLoggingConfig() *LoggingConfig { } } -func (logcfg *LoggingConfig) validateBasic() error { +func (logcfg *LogConfig) validateBasic() error { return nil } // GetLoggingConfig provides logging-specific configuration, from top-level Config. func (cfg *Config) GetLoggingConfig() (logging.Config, error) { var loglvl logging.LogLevel - switch cfg.Logging.Level { + switch cfg.Log.Level { case "debug": loglvl = logging.Debug case "info": @@ -361,22 +361,22 @@ func (cfg *Config) GetLoggingConfig() (logging.Config, error) { case "fatal": loglvl = logging.Fatal default: - return logging.Config{}, fmt.Errorf("invalid log level: %s", cfg.Logging.Level) + return logging.Config{}, fmt.Errorf("invalid log level: %s", cfg.Log.Level) } var encfmt logging.EncoderFormat - switch cfg.Logging.Format { + switch cfg.Log.Format { case "json": encfmt = logging.JSON case "csv": encfmt = logging.CSV default: - return logging.Config{}, fmt.Errorf("invalid log format: %s", cfg.Logging.Format) + return logging.Config{}, fmt.Errorf("invalid log format: %s", cfg.Log.Format) } return logging.Config{ Level: logging.NewLogLevelOption(loglvl), - EnableStackTrace: logging.NewEnableStackTraceOption(cfg.Logging.Stacktrace), + EnableStackTrace: logging.NewEnableStackTraceOption(cfg.Log.Stacktrace), EncoderFormat: logging.NewEncoderFormatOption(encfmt), - OutputPaths: []string{cfg.Logging.OutputPath}, + OutputPaths: []string{cfg.Log.OutputPath}, }, nil } diff --git a/config/config_test.go b/config/config_test.go index b3e5fb4a2f..a9c9deb163 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -34,9 +34,9 @@ var envVarsDifferentThanDefault = map[string]string{ "DEFRA_NET_RPCTIMEOUT": "90s", "DEFRA_NET_PUBSUB": "false", "DEFRA_NET_RELAY": "false", - "DEFRA_LOGGING_LEVEL": "info", - "DEFRA_LOGGING_STACKTRACE": "false", - "DEFRA_LOGGING_FORMAT": "json", + "DEFRA_LOG_LEVEL": "info", + "DEFRA_LOG_STACKTRACE": "false", + "DEFRA_LOG_FORMAT": "json", } var envVarsInvalid = map[string]string{ @@ -49,9 +49,9 @@ var envVarsInvalid = map[string]string{ "DEFRA_NET_RPCTIMEOUT": "^=+()&**()*(&))", "DEFRA_NET_PUBSUB": "^=+()&**()*(&))", "DEFRA_NET_RELAY": "^=+()&**()*(&))", - "DEFRA_LOGGING_LEVEL": "^=+()&**()*(&))", - "DEFRA_LOGGING_STACKTRACE": "^=+()&**()*(&))", - "DEFRA_LOGGING_FORMAT": "^=+()&**()*(&))", + "DEFRA_LOG_LEVEL": "^=+()&**()*(&))", + "DEFRA_LOG_STACKTRACE": "^=+()&**()*(&))", + "DEFRA_LOG_FORMAT": "^=+()&**()*(&))", } func FixtureEnvVars(envVars map[string]string) { @@ -147,9 +147,9 @@ func TestEnvVariablesAllConsidered(t *testing.T) { assert.Equal(t, "90s", cfg.Net.RPCTimeout) assert.Equal(t, false, cfg.Net.PubSubEnabled) assert.Equal(t, false, cfg.Net.RelayEnabled) - assert.Equal(t, "info", cfg.Logging.Level) - assert.Equal(t, false, cfg.Logging.Stacktrace) - assert.Equal(t, "json", cfg.Logging.Format) + assert.Equal(t, "info", cfg.Log.Level) + assert.Equal(t, false, cfg.Log.Stacktrace) + assert.Equal(t, "json", cfg.Log.Format) } func TestGetRootDirExists(t *testing.T) { @@ -275,10 +275,10 @@ func TestInvalidMaxConnectionIdleDuration(t *testing.T) { func TestGetLoggingConfig(t *testing.T) { cfg := DefaultConfig() - cfg.Logging.Level = "debug" - cfg.Logging.Format = "json" - cfg.Logging.Stacktrace = true - cfg.Logging.OutputPath = "stdout" + cfg.Log.Level = "debug" + cfg.Log.Format = "json" + cfg.Log.Stacktrace = true + cfg.Log.OutputPath = "stdout" loggingConfig, err := cfg.GetLoggingConfig() @@ -291,8 +291,8 @@ func TestGetLoggingConfig(t *testing.T) { func TestInvalidGetLoggingConfig(t *testing.T) { cfg := DefaultConfig() - cfg.Logging.Level = "546578" - cfg.Logging.Format = "*&)*&" + cfg.Log.Level = "546578" + cfg.Log.Format = "*&)*&" cfg.LoadWithoutRootDir() _, err := cfg.GetLoggingConfig() diff --git a/config/configfile.go b/config/configfile.go index aa69c88ad1..f46939f84f 100644 --- a/config/configfile.go +++ b/config/configfile.go @@ -81,15 +81,15 @@ net: # Amount of time after which an idle RPC connection would be closed RPCMaxConnectionIdle: {{ .Net.RPCMaxConnectionIdle }} -logging: +log: # Log level. Options are debug, info, error, fatal - level: {{ .Logging.Level }} + level: {{ .Log.Level }} # Include stacktrace in error and fatal logs - stacktrace: {{ .Logging.Stacktrace }} + stacktrace: {{ .Log.Stacktrace }} # Supported log formats are json, csv - format: {{ .Logging.Format }} + format: {{ .Log.Format }} # Where the log output is written to - outputpath: {{ .Logging.OutputPath }} + output: {{ .Log.OutputPath }} # Color the log output - color: {{ .Logging.Color }} + color: {{ .Log.Color }} `