From 31dfa603868a29632e4a4756f19b206604db6f77 Mon Sep 17 00:00:00 2001
From: Wolfgang Welz <welzwo@gmail.com>
Date: Wed, 8 Jan 2020 20:43:29 +0100
Subject: [PATCH] fix: integrate autopeering logging into global logger

---
 packages/autopeering/logger/logger.go | 60 ---------------------------
 plugins/autopeering/autopeering.go    | 33 +++------------
 plugins/autopeering/plugin.go         | 11 ++---
 plugins/cli/plugin.go                 |  2 +-
 plugins/gossip/gossip.go              | 30 ++------------
 plugins/gossip/plugin.go              | 12 +++---
 6 files changed, 20 insertions(+), 128 deletions(-)
 delete mode 100644 packages/autopeering/logger/logger.go

diff --git a/packages/autopeering/logger/logger.go b/packages/autopeering/logger/logger.go
deleted file mode 100644
index 9a14bb05..00000000
--- a/packages/autopeering/logger/logger.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package logger
-
-import (
-	"encoding/json"
-	"fmt"
-
-	"go.uber.org/zap"
-	"go.uber.org/zap/zapcore"
-)
-
-// NewLogger creates a logger with the supplied configuration.
-func NewLogger(configJSON string, levelOverride string, opts ...zap.Option) *zap.SugaredLogger {
-	var loggingCfg zap.Config
-	if err := json.Unmarshal([]byte(configJSON), &loggingCfg); err != nil {
-		return newFallbackLogger(err, levelOverride, opts...)
-	}
-	if len(levelOverride) > 0 {
-		if level, err := levelFromString(levelOverride); err == nil {
-			loggingCfg.Level = zap.NewAtomicLevelAt(*level)
-		}
-	}
-
-	logger, err := loggingCfg.Build(opts...)
-	if err != nil {
-		return newFallbackLogger(err, levelOverride, opts...)
-	}
-
-	logger.Info("Successfully created the logger.")
-	logger.Sugar().Infof("Logging level set to %v", loggingCfg.Level)
-
-	return logger.Sugar()
-}
-
-func levelFromString(level string) (*zapcore.Level, error) {
-	var zapLevel zapcore.Level
-	if err := zapLevel.UnmarshalText([]byte(level)); err != nil {
-		return nil, fmt.Errorf("invalid logging level: %v", level)
-	}
-	return &zapLevel, nil
-}
-
-func newFallbackLogger(cause error, levelOverride string, opts ...zap.Option) *zap.SugaredLogger {
-	loggingCfg := zap.NewProductionConfig()
-	if len(levelOverride) > 0 {
-		if level, err := levelFromString(levelOverride); err == nil {
-			loggingCfg.Level = zap.NewAtomicLevelAt(*level)
-		}
-	}
-
-	logger, err := loggingCfg.Build(opts...)
-	if err != nil {
-		panic(err)
-	}
-	logger = logger.Named("fallback-logger")
-
-	logger.Warn("Failed to create logger, using fallback:", zap.Error(cause))
-	logger.Sugar().Infof("Logging level set to %v", loggingCfg.Level)
-
-	return logger.Sugar()
-}
diff --git a/plugins/autopeering/autopeering.go b/plugins/autopeering/autopeering.go
index 3bbbd339..4d70e09e 100644
--- a/plugins/autopeering/autopeering.go
+++ b/plugins/autopeering/autopeering.go
@@ -7,7 +7,6 @@ import (
 	"strings"
 
 	"github.com/iotaledger/goshimmer/packages/autopeering/discover"
-	"github.com/iotaledger/goshimmer/packages/autopeering/logger"
 	"github.com/iotaledger/goshimmer/packages/autopeering/peer"
 	"github.com/iotaledger/goshimmer/packages/autopeering/peer/service"
 	"github.com/iotaledger/goshimmer/packages/autopeering/selection"
@@ -15,6 +14,7 @@ import (
 	"github.com/iotaledger/goshimmer/packages/autopeering/transport"
 	"github.com/iotaledger/goshimmer/packages/parameter"
 	"github.com/iotaledger/goshimmer/plugins/autopeering/local"
+	"github.com/iotaledger/hive.go/logger"
 	"github.com/pkg/errors"
 )
 
@@ -23,30 +23,9 @@ var (
 	Discovery *discover.Protocol
 	// Selection is the peer selection protocol.
 	Selection *selection.Protocol
-)
-
-const defaultZLC = `{
-	"level": "info",
-	"development": false,
-	"outputPaths": ["./autopeering.log"],
-	"errorOutputPaths": ["stderr"],
-	"encoding": "console",
-	"encoderConfig": {
-	  "timeKey": "ts",
-	  "levelKey": "level",
-	  "nameKey": "logger",
-	  "callerKey": "caller",
-	  "messageKey": "msg",
-	  "stacktraceKey": "stacktrace",
-	  "lineEnding": "",
-	  "levelEncoder": "",
-	  "timeEncoder": "iso8601",
-	  "durationEncoder": "",
-	  "callerEncoder": ""
-	}
-  }`
 
-var zLogger = logger.NewLogger(defaultZLC, logLevel)
+	log *logger.Logger
+)
 
 func configureAP() {
 	masterPeers, err := parseEntryNodes()
@@ -56,13 +35,13 @@ func configureAP() {
 	log.Debugf("Master peers: %v", masterPeers)
 
 	Discovery = discover.New(local.GetInstance(), discover.Config{
-		Log:         zLogger.Named("disc"),
+		Log:         log.Named("disc"),
 		MasterPeers: masterPeers,
 	})
 
 	if parameter.NodeConfig.GetBool(CFG_SELECTION) {
 		Selection = selection.New(local.GetInstance(), Discovery, selection.Config{
-			Log: zLogger.Named("sel"),
+			Log: log.Named("sel"),
 			Param: &selection.Parameters{
 				SaltLifetime:    selection.DefaultSaltLifetime,
 				RequiredService: []service.Key{service.GossipKey},
@@ -104,7 +83,7 @@ func start(shutdownSignal <-chan struct{}) {
 	}
 
 	// start a server doing discovery and peering
-	srv := server.Listen(local.GetInstance(), trans, zLogger.Named("srv"), handlers...)
+	srv := server.Listen(local.GetInstance(), trans, log.Named("srv"), handlers...)
 	defer srv.Close()
 
 	// start the discovery on that connection
diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go
index 1952d5c7..da2fd920 100644
--- a/plugins/autopeering/plugin.go
+++ b/plugins/autopeering/plugin.go
@@ -9,15 +9,10 @@ import (
 	"github.com/iotaledger/hive.go/node"
 )
 
-const (
-	name     = "Autopeering" // name of the plugin
-	logLevel = "info"
-)
+const name = "Autopeering" // name of the plugin
 
 var PLUGIN = node.NewPlugin(name, node.Enabled, configure, run)
 
-var log *logger.Logger
-
 func configure(*node.Plugin) {
 	log = logger.NewLogger(name)
 
@@ -26,7 +21,9 @@ func configure(*node.Plugin) {
 }
 
 func run(*node.Plugin) {
-	daemon.BackgroundWorker(name, start)
+	if err := daemon.BackgroundWorker(name, start); err != nil {
+		log.Errorf("Failed to start as daemon: %s", err)
+	}
 }
 
 func configureEvents() {
diff --git a/plugins/cli/plugin.go b/plugins/cli/plugin.go
index fe3bbc0a..39791c7c 100644
--- a/plugins/cli/plugin.go
+++ b/plugins/cli/plugin.go
@@ -44,7 +44,7 @@ func parseParameters() {
 	}
 }
 
-func LoadConfig(){
+func LoadConfig() {
 	if err := parameter.FetchConfig(true); err != nil {
 		panic(err)
 	}
diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go
index 8b552c69..aff925c7 100644
--- a/plugins/gossip/gossip.go
+++ b/plugins/gossip/gossip.go
@@ -5,7 +5,6 @@ import (
 	"net"
 	"strconv"
 
-	"github.com/iotaledger/goshimmer/packages/autopeering/logger"
 	"github.com/iotaledger/goshimmer/packages/autopeering/peer/service"
 	"github.com/iotaledger/goshimmer/packages/errors"
 	gp "github.com/iotaledger/goshimmer/packages/gossip"
@@ -13,36 +12,15 @@ import (
 	"github.com/iotaledger/goshimmer/packages/parameter"
 	"github.com/iotaledger/goshimmer/plugins/autopeering/local"
 	"github.com/iotaledger/goshimmer/plugins/tangle"
+	"github.com/iotaledger/hive.go/logger"
 	"github.com/iotaledger/hive.go/typeutils"
 )
 
 var (
+	log *logger.Logger
 	mgr *gp.Manager
 )
 
-const defaultZLC = `{
-	"level": "info",
-	"development": false,
-	"outputPaths": ["./gossip.log"],
-	"errorOutputPaths": ["stderr"],
-	"encoding": "console",
-	"encoderConfig": {
-	  "timeKey": "ts",
-	  "levelKey": "level",
-	  "nameKey": "logger",
-	  "callerKey": "caller",
-	  "messageKey": "msg",
-	  "stacktraceKey": "stacktrace",
-	  "lineEnding": "",
-	  "levelEncoder": "",
-	  "timeEncoder": "iso8601",
-	  "durationEncoder": "",
-	  "callerEncoder": ""
-	}
-  }`
-
-var zLogger = logger.NewLogger(defaultZLC, logLevel)
-
 func configureGossip() {
 	lPeer := local.GetInstance()
 
@@ -57,13 +35,13 @@ func configureGossip() {
 		log.Fatalf("could not update services: %v", err)
 	}
 
-	mgr = gp.NewManager(lPeer, getTransaction, zLogger)
+	mgr = gp.NewManager(lPeer, getTransaction, log)
 }
 
 func start(shutdownSignal <-chan struct{}) {
 	defer log.Info("Stopping Gossip ... done")
 
-	srv, err := server.ListenTCP(local.GetInstance(), zLogger)
+	srv, err := server.ListenTCP(local.GetInstance(), log)
 	if err != nil {
 		log.Fatalf("ListenTCP: %v", err)
 	}
diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go
index 2c6d6a8d..77436467 100644
--- a/plugins/gossip/plugin.go
+++ b/plugins/gossip/plugin.go
@@ -13,23 +13,21 @@ import (
 	"github.com/iotaledger/hive.go/typeutils"
 )
 
-const (
-	name     = "Gossip" // name of the plugin
-	logLevel = "info"
-)
+const name = "Gossip" // name of the plugin
 
 var PLUGIN = node.NewPlugin(name, node.Enabled, configure, run)
 
-var log *logger.Logger
-
 func configure(*node.Plugin) {
 	log = logger.NewLogger(name)
+
 	configureGossip()
 	configureEvents()
 }
 
 func run(*node.Plugin) {
-	daemon.BackgroundWorker(name, start)
+	if err := daemon.BackgroundWorker(name, start); err != nil {
+		log.Errorf("Failed to start as daemon: %s", err)
+	}
 }
 
 func configureEvents() {
-- 
GitLab