Skip to content

Commit

Permalink
fix: Consider --logoutput CLI flag properly (sourcenetwork#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Jul 19, 2022
1 parent 0195a74 commit 44556cb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
30 changes: 15 additions & 15 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 15 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -141,7 +141,7 @@ func DefaultConfig() *Config {
Datastore: defaultDatastoreConfig(),
API: defaultAPIConfig(),
Net: defaultNetConfig(),
Logging: defaultLoggingConfig(),
Log: defaultLogConfig(),
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -325,17 +325,17 @@ 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
OutputPath string // logging actually supports multiple output paths, but here only one is supported
Color bool
}

func defaultLoggingConfig() *LoggingConfig {
return &LoggingConfig{
func defaultLogConfig() *LogConfig {
return &LogConfig{
Level: "info",
Stacktrace: false,
Format: "csv",
Expand All @@ -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":
Expand All @@ -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
}

Expand Down
30 changes: 15 additions & 15 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
`

0 comments on commit 44556cb

Please sign in to comment.