Merge pull request #7831 from kzys/fix-race

This commit is contained in:
Fu Wei 2022-12-20 20:37:50 +08:00 committed by GitHub
commit dd5605e444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ package logtest
import (
"bytes"
"sync"
"testing"
"github.com/sirupsen/logrus"
@ -26,6 +27,7 @@ import (
type testHook struct {
t testing.TB
fmt logrus.Formatter
mu sync.Mutex
}
func (*testHook) Levels() []logrus.Level {
@ -37,6 +39,12 @@ func (h *testHook) Fire(e *logrus.Entry) error {
if err != nil {
return err
}
// Because the logger could be called from multiple goroutines,
// but t.Log() is not designed for.
h.mu.Lock()
defer h.mu.Unlock()
h.t.Log(string(bytes.TrimRight(s, "\n")))
return nil
}