diff --git a/packages/autopeering/logger/logger.go b/packages/autopeering/logger/logger.go deleted file mode 100644 index 9a14bb0586fe79f4911be65729ffe355a894cf20..0000000000000000000000000000000000000000 --- 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 3bbbd33957760e63177e1cdc4de8e873d0cd14b4..4d70e09eae26cda600a67d09abaa2b8374b561f8 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 1952d5c7d9c9de41d4f540d1020ef5eaa8d87ccb..da2fd92088708150e6d487e0be412ec8c8e0190b 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 fe3bbc0aa2ca42daa381dab19878fb1d0bbf2a20..39791c7c52a7e8f0f9506d4729f83e9414125c61 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 8b552c69f800972eea094f5da6a1b6678a3ce056..aff925c7b9622f0d76b5417e54e9344b43a91f71 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 2c6d6a8d0cdba30f25f3d010fb6147678088c321..774364674fd7069c241f7d460b9c629ec5d8c059 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() {