diff --git a/config/viper.go b/config/viper.go index c8f308f5368f0ed9070ad6fc77270eb306cf32c2..c59799a80210f0623a54eb674de0971307383290 100644 --- a/config/viper.go +++ b/config/viper.go @@ -49,7 +49,7 @@ func ViperSubConfig(conf *viper.Viper, configSubtreePath string) (subConfig *vip func ReadViperConfig(configBytes []byte) (*viper.Viper, error) { buf := bytes.NewBuffer(configBytes) conf := viper.New() - viper.SetConfigType("toml") + conf.SetConfigType("toml") err := conf.ReadConfig(buf) if err != nil { return nil, err diff --git a/core/config.go b/core/config.go index 8c602c716873ac39b064d5c4066dfcb6c810ee18..bddb21b8491f69ed719230ceba412aa2c34aa028 100644 --- a/core/config.go +++ b/core/config.go @@ -107,6 +107,9 @@ func LoadServerConfig(chainId string, rootConfig *viper.Viper) (*server.ServerCo } func LoadLoggingConfigFromDo(do *definitions.Do) (*lconfig.LoggingConfig, error) { + if !do.Config.IsSet("logging") { + return nil, nil + } loggingConfigMap := do.Config.GetStringMap("logging") return lconfig.LoggingConfigFromMap(loggingConfigMap) } diff --git a/core/config_test.go b/core/config_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ef552d2b0581d48538e6119a2b5615474fe05de4 --- /dev/null +++ b/core/config_test.go @@ -0,0 +1,25 @@ +package core + +import ( + "testing" + + "github.com/hyperledger/burrow/config" + "github.com/hyperledger/burrow/definitions" + lconfig "github.com/hyperledger/burrow/logging/config" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestLoadLoggingConfigFromDo(t *testing.T) { + do := new(definitions.Do) + do.Config = viper.New() + lc, err := LoadLoggingConfigFromDo(do) + assert.NoError(t, err) + assert.Nil(t, lc, "Should get nil logging config when [logging] not set") + cnf, err := config.ReadViperConfig(([]byte)(lconfig.DefaultNodeLoggingConfig().RootTOMLString())) + assert.NoError(t, err) + do.Config = cnf + lc, err = LoadLoggingConfigFromDo(do) + assert.NoError(t, err) + assert.EqualValues(t, lconfig.DefaultNodeLoggingConfig(), lc) +} diff --git a/logging/lifecycle/lifecycle_test.go b/logging/lifecycle/lifecycle_test.go index bacb97ebe5924c85dba533da224018e3c559efe5..9c88ce3b47fd4ffbb7d34050e58473f425c4d4d1 100644 --- a/logging/lifecycle/lifecycle_test.go +++ b/logging/lifecycle/lifecycle_test.go @@ -1,14 +1,30 @@ package lifecycle import ( - "runtime" + "os" "testing" - "time" + + "bufio" + + "github.com/stretchr/testify/assert" ) -func TestNewStdErrLogger(t *testing.T) { - logger := NewStdErrLogger() +func TestNewLoggerFromLoggingConfig(t *testing.T) { + stderr := os.Stderr + defer func() { + os.Stderr = stderr + }() + r, w, err := os.Pipe() + assert.NoError(t, err, "Couldn't make fifo") + os.Stderr = w + logger, err := NewLoggerFromLoggingConfig(nil) + assert.NoError(t, err) logger.Info("Quick", "Test") - time.Sleep(time.Second) - runtime.Gosched() + reader := bufio.NewReader(r) + assert.NoError(t, err) + line, _, err := reader.ReadLine() + assert.NoError(t, err) + // This test shouldn't really depend on colour codes, if you find yourself + // changing it then assert.NotEmpty() should do + assert.Contains(t, string(line), "\x1b[34mQuick\x1b[0m=Test") }