raft: use mutex in "SetLogger" to avoid race conditions in tests
@@ -19,6 +19,7 @@ import (
"io/ioutil"
"log"
"os"
+ "sync"
)
type Logger interface {
@@ -41,11 +42,16 @@ type Logger interface {
Panicf(format string, v ...interface{})
}
-func SetLogger(l Logger) { raftLogger = l }
+func SetLogger(l Logger) {
+ raftLoggerMu.Lock()
+ raftLogger = l
+ raftLoggerMu.Unlock()
+}
var (
defaultLogger = &DefaultLogger{Logger: log.New(os.Stderr, "raft", log.LstdFlags)}
discardLogger = &DefaultLogger{Logger: log.New(ioutil.Discard, "", 0)}
+ raftLoggerMu sync.Mutex
raftLogger = Logger(defaultLogger)
)
