From 93330c8e3c199330d8d6d4592525610dd9957c62 Mon Sep 17 00:00:00 2001
From: Silas Davis <silas@erisindustries.com>
Date: Fri, 25 Aug 2017 16:40:40 +0100
Subject: [PATCH] Make sure we log somewhere if no config supplied

---
 config/viper.go                     |  2 +-
 core/config.go                      |  3 +++
 core/config_test.go                 | 25 +++++++++++++++++++++++++
 logging/lifecycle/lifecycle_test.go | 28 ++++++++++++++++++++++------
 4 files changed, 51 insertions(+), 7 deletions(-)
 create mode 100644 core/config_test.go

diff --git a/config/viper.go b/config/viper.go
index c8f308f5..c59799a8 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 8c602c71..bddb21b8 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 00000000..ef552d2b
--- /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 bacb97eb..9c88ce3b 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")
 }
-- 
GitLab