open-consul/testutil/testlog.go
R.B. Boyer b5d71ea779
testutil: redirect some test agent logs to testing.T.Logf (#5304)
When tests fail, only the logs for the failing run are dumped to the
console which helps in diagnosis. This is easily added to other test
scenarios as they come up.
2019-02-01 09:21:54 -06:00

31 lines
550 B
Go

package testutil
import (
"io"
"log"
"strings"
"testing"
)
func TestLogger(t testing.TB) *log.Logger {
return log.New(&testWriter{t}, "test: ", log.LstdFlags)
}
func TestLoggerWithName(t testing.TB, name string) *log.Logger {
return log.New(&testWriter{t}, "test["+name+"]: ", log.LstdFlags)
}
func TestWriter(t testing.TB) io.Writer {
return &testWriter{t}
}
type testWriter struct {
t testing.TB
}
func (tw *testWriter) Write(p []byte) (n int, err error) {
tw.t.Helper()
tw.t.Log(strings.TrimSpace(string(p)))
return len(p), nil
}