Improve raft log forwarder
It was very idiotic poll a buffer/read it and then write rather than just have a custom writer. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
This commit is contained in:
parent
0e7091c6cb
commit
9deb56b762
29
logging.go
29
logging.go
|
@ -1,17 +1,14 @@
|
||||||
package ipfscluster
|
package ipfscluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = logging.Logger("cluster")
|
var logger = logging.Logger("cluster")
|
||||||
var raftStdLogger = makeRaftLogger()
|
var raftStdLogger = log.New(&logForwarder{}, "", 0)
|
||||||
var raftLogger = logging.Logger("raft")
|
var raftLogger = logging.Logger("raft")
|
||||||
|
|
||||||
// SetFacilityLogLevel sets the log level for a given module
|
// SetFacilityLogLevel sets the log level for a given module
|
||||||
|
@ -27,20 +24,14 @@ func SetFacilityLogLevel(f, l string) {
|
||||||
logging.SetLogLevel(f, l)
|
logging.SetLogLevel(f, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This redirects Raft output to our logger
|
// implements the writer interface
|
||||||
func makeRaftLogger() *log.Logger {
|
type logForwarder struct{}
|
||||||
var buf bytes.Buffer
|
|
||||||
rLogger := log.New(&buf, "", 0)
|
|
||||||
reader := bufio.NewReader(&buf)
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
t, err := reader.ReadString('\n')
|
|
||||||
if err != nil {
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t = strings.TrimSuffix(t, "\n")
|
|
||||||
|
|
||||||
|
// Write forwards to our go-log logger.
|
||||||
|
// According to https://golang.org/pkg/log/#Logger.Output
|
||||||
|
// it is called per line.
|
||||||
|
func (fw *logForwarder) Write(p []byte) (n int, err error) {
|
||||||
|
t := strings.TrimSuffix(string(p), "\n")
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(t, "[DEBUG]"):
|
case strings.Contains(t, "[DEBUG]"):
|
||||||
raftLogger.Debug(strings.TrimPrefix(t, "[DEBUG] raft: "))
|
raftLogger.Debug(strings.TrimPrefix(t, "[DEBUG] raft: "))
|
||||||
|
@ -53,7 +44,5 @@ func makeRaftLogger() *log.Logger {
|
||||||
default:
|
default:
|
||||||
raftLogger.Debug(t)
|
raftLogger.Debug(t)
|
||||||
}
|
}
|
||||||
}
|
return len(p), nil
|
||||||
}()
|
|
||||||
return rLogger
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user