~samwhited/testlog

ref: v0.0.1 testlog/testlog.go -rw-r--r-- 688 bytes
e7c6a33dSam Whited Initial public release 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Package testlog is a log.Logger that proxies to the Log function on a
// testing.T.
//
// It is used to group log messages under the tests that generated them in test
// output and to only show those messages if the test that generated them
// failed.
package testlog // import "code.soquee.net/testlog"

import (
	"log"
	"testing"
)

// New returns a new logger that logs to the provided testing.T.
func New(t testing.TB) *log.Logger {
	t.Helper()
	return log.New(testWriter{TB: t}, t.Name()+" ", log.LstdFlags|log.Lshortfile|log.LUTC)
}

type testWriter struct {
	testing.TB
}

func (tw testWriter) Write(p []byte) (int, error) {
	tw.Helper()
	tw.Logf("%s", p)
	return len(p), nil
}