Skip to content

Commit

Permalink
fix race condition in testlogger
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-r-reis committed May 29, 2024
1 parent fd4b484 commit d8c31b4
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"strconv"
"strings"
"sync"
)

type StdLogger interface {
Expand All @@ -32,12 +33,32 @@ func (n nopLogger) Debug(_ string, _ ...LogField) {}

type testLogger struct {
capture bytes.Buffer
mu sync.Mutex
}

func (l *testLogger) Print(v ...interface{}) { fmt.Fprint(&l.capture, v...) }
func (l *testLogger) Printf(format string, v ...interface{}) { fmt.Fprintf(&l.capture, format, v...) }
func (l *testLogger) Println(v ...interface{}) { fmt.Fprintln(&l.capture, v...) }
func (l *testLogger) String() string { return l.capture.String() }
func (l *testLogger) Print(v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(&l.capture, v...)
}

func (l *testLogger) Printf(format string, v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintf(&l.capture, format, v...)
}

func (l *testLogger) Println(v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintln(&l.capture, v...)
}

func (l *testLogger) String() string {
l.mu.Lock()
defer l.mu.Unlock()
return l.capture.String()
}

type defaultLogger struct{}

Expand Down

0 comments on commit d8c31b4

Please sign in to comment.