Skip to content

Commit 0218d70

Browse files
fjlkaralabe
authored andcommitted
internal/testlog: print file+line number of log call in test log (#20528)
* internal/testlog: print file+line number of log call in test log This changes the unit test logger to print the actual file and line number of the logging call instead of "testlog.go:44". Output of 'go test -v -run TestServerListen ./p2p' before this change: === RUN TestServerListen --- PASS: TestServerListen (0.00s) testlog.go:44: DEBUG[01-08|15:16:31.651] UDP listener up addr=127.0.0.1:62678 testlog.go:44: DEBUG[01-08|15:16:31.651] TCP listener up addr=127.0.0.1:62678 testlog.go:44: TRACE[01-08|15:16:31.652] Accepted connection addr=127.0.0.1:62679 And after: === RUN TestServerListen --- PASS: TestServerListen (0.00s) server.go:868: DEBUG[01-08|15:25:35.679] TCP listener up addr=127.0.0.1:62712 server.go:557: DEBUG[01-08|15:25:35.679] UDP listener up addr=127.0.0.1:62712 server.go:912: TRACE[01-08|15:25:35.680] Accepted connection addr=127.0.0.1:62713 * internal/testlog: document use of t.Helper
1 parent 4d663d5 commit 0218d70

File tree

1 file changed

+103
-7
lines changed

1 file changed

+103
-7
lines changed

internal/testlog/testlog.go

+103-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818
package testlog
1919

2020
import (
21+
"sync"
2122
"testing"
2223

2324
"github.com/ethereum/go-ethereum/log"
2425
)
2526

26-
// Logger returns a logger which logs to the unit test log of t.
27-
func Logger(t *testing.T, level log.Lvl) log.Logger {
28-
l := log.New()
29-
l.SetHandler(Handler(t, level))
30-
return l
31-
}
32-
3327
// Handler returns a log handler which logs to the unit test log of t.
3428
func Handler(t *testing.T, level log.Lvl) log.Handler {
3529
return log.LvlFilterHandler(level, &handler{t, log.TerminalFormat(false)})
@@ -44,3 +38,105 @@ func (h *handler) Log(r *log.Record) error {
4438
h.t.Logf("%s", h.fmt.Format(r))
4539
return nil
4640
}
41+
42+
// logger implements log.Logger such that all output goes to the unit test log via
43+
// t.Logf(). All methods in between logger.Trace, logger.Debug, etc. are marked as test
44+
// helpers, so the file and line number in unit test output correspond to the call site
45+
// which emitted the log message.
46+
type logger struct {
47+
t *testing.T
48+
l log.Logger
49+
mu *sync.Mutex
50+
h *bufHandler
51+
}
52+
53+
type bufHandler struct {
54+
buf []*log.Record
55+
fmt log.Format
56+
}
57+
58+
func (h *bufHandler) Log(r *log.Record) error {
59+
h.buf = append(h.buf, r)
60+
return nil
61+
}
62+
63+
// Logger returns a logger which logs to the unit test log of t.
64+
func Logger(t *testing.T, level log.Lvl) log.Logger {
65+
l := &logger{
66+
t: t,
67+
l: log.New(),
68+
mu: new(sync.Mutex),
69+
h: &bufHandler{fmt: log.TerminalFormat(false)},
70+
}
71+
l.l.SetHandler(log.LvlFilterHandler(level, l.h))
72+
return l
73+
}
74+
75+
func (l *logger) Trace(msg string, ctx ...interface{}) {
76+
l.t.Helper()
77+
l.mu.Lock()
78+
defer l.mu.Unlock()
79+
l.l.Trace(msg, ctx...)
80+
l.flush()
81+
}
82+
83+
func (l *logger) Debug(msg string, ctx ...interface{}) {
84+
l.t.Helper()
85+
l.mu.Lock()
86+
defer l.mu.Unlock()
87+
l.l.Debug(msg, ctx...)
88+
l.flush()
89+
}
90+
91+
func (l *logger) Info(msg string, ctx ...interface{}) {
92+
l.t.Helper()
93+
l.mu.Lock()
94+
defer l.mu.Unlock()
95+
l.l.Info(msg, ctx...)
96+
l.flush()
97+
}
98+
99+
func (l *logger) Warn(msg string, ctx ...interface{}) {
100+
l.t.Helper()
101+
l.mu.Lock()
102+
defer l.mu.Unlock()
103+
l.l.Warn(msg, ctx...)
104+
l.flush()
105+
}
106+
107+
func (l *logger) Error(msg string, ctx ...interface{}) {
108+
l.t.Helper()
109+
l.mu.Lock()
110+
defer l.mu.Unlock()
111+
l.l.Error(msg, ctx...)
112+
l.flush()
113+
}
114+
115+
func (l *logger) Crit(msg string, ctx ...interface{}) {
116+
l.t.Helper()
117+
l.mu.Lock()
118+
defer l.mu.Unlock()
119+
l.l.Crit(msg, ctx...)
120+
l.flush()
121+
}
122+
123+
func (l *logger) New(ctx ...interface{}) log.Logger {
124+
return &logger{l.t, l.l.New(ctx...), l.mu, l.h}
125+
}
126+
127+
func (l *logger) GetHandler() log.Handler {
128+
return l.l.GetHandler()
129+
}
130+
131+
func (l *logger) SetHandler(h log.Handler) {
132+
l.l.SetHandler(h)
133+
}
134+
135+
// flush writes all buffered messages and clears the buffer.
136+
func (l *logger) flush() {
137+
l.t.Helper()
138+
for _, r := range l.h.buf {
139+
l.t.Logf("%s", l.h.fmt.Format(r))
140+
}
141+
l.h.buf = nil
142+
}

0 commit comments

Comments
 (0)