Browse Source

networkdb: Properly format memberlist logs

Right now, items logged by memberlist end up as a complete log line
embedded inside another log line, like the following:

    Nov 22 16:34:16 hostname dockerd: time="2016-11-22T16:34:16.802103258-08:00" level=info msg="2016/11/22 16:34:16 [INFO] memberlist: Marking xyz-1d1ec2dfa053 as failed, suspect timeout reached\n"

This has two time and date stamps, and an escaped newline inside the
"msg" field of the outer log message.

To fix this, define a custom logger that only prints the message itself.
Capture this message in logWriter, strip off the log level (added
directly by memberlist), and route to the appropriate logrus method.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
Aaron Lehmann 8 years ago
parent
commit
bb8b9a6040
1 changed files with 13 additions and 5 deletions
  1. 13 5
      libnetwork/networkdb/cluster.go

+ 13 - 5
libnetwork/networkdb/cluster.go

@@ -5,6 +5,7 @@ import (
 	"crypto/rand"
 	"crypto/rand"
 	"encoding/hex"
 	"encoding/hex"
 	"fmt"
 	"fmt"
+	"log"
 	"math/big"
 	"math/big"
 	rnd "math/rand"
 	rnd "math/rand"
 	"net"
 	"net"
@@ -27,15 +28,20 @@ type logWriter struct{}
 
 
 func (l *logWriter) Write(p []byte) (int, error) {
 func (l *logWriter) Write(p []byte) (int, error) {
 	str := string(p)
 	str := string(p)
+	str = strings.TrimSuffix(str, "\n")
 
 
 	switch {
 	switch {
-	case strings.Contains(str, "[WARN]"):
+	case strings.HasPrefix(str, "[WARN] "):
+		str = strings.TrimPrefix(str, "[WARN] ")
 		logrus.Warn(str)
 		logrus.Warn(str)
-	case strings.Contains(str, "[DEBUG]"):
+	case strings.HasPrefix(str, "[DEBUG] "):
+		str = strings.TrimPrefix(str, "[DEBUG] ")
 		logrus.Debug(str)
 		logrus.Debug(str)
-	case strings.Contains(str, "[INFO]"):
+	case strings.HasPrefix(str, "[INFO] "):
+		str = strings.TrimPrefix(str, "[INFO] ")
 		logrus.Info(str)
 		logrus.Info(str)
-	case strings.Contains(str, "[ERR]"):
+	case strings.HasPrefix(str, "[ERR] "):
+		str = strings.TrimPrefix(str, "[ERR] ")
 		logrus.Warn(str)
 		logrus.Warn(str)
 	}
 	}
 
 
@@ -104,7 +110,9 @@ func (nDB *NetworkDB) clusterInit() error {
 	config.ProtocolVersion = memberlist.ProtocolVersionMax
 	config.ProtocolVersion = memberlist.ProtocolVersionMax
 	config.Delegate = &delegate{nDB: nDB}
 	config.Delegate = &delegate{nDB: nDB}
 	config.Events = &eventDelegate{nDB: nDB}
 	config.Events = &eventDelegate{nDB: nDB}
-	config.LogOutput = &logWriter{}
+	// custom logger that does not add time or date, so they are not
+	// duplicated by logrus
+	config.Logger = log.New(&logWriter{}, "", 0)
 
 
 	var err error
 	var err error
 	if len(nDB.config.Keys) > 0 {
 	if len(nDB.config.Keys) > 0 {