Skip to content

Commit 6c5d3fe

Browse files
committed
Simplify test logger interface
1 parent ff242a8 commit 6c5d3fe

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

internal/stdlibctx/stdlibctx.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

slog.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ func Make(s Sink) Logger {
159159
}
160160
l.SetLevel(LevelDebug)
161161

162-
if sink, ok := s.(interface {
163-
XXX_slogTestingHelper() func()
164-
}); ok {
165-
l.testingHelper = sink.XXX_slogTestingHelper()
162+
if ts, ok := s.(testSink); ok {
163+
l.testingHelper = ts.TestingHelper()
166164
}
167165
return l
168166
}
@@ -180,6 +178,11 @@ type Logger struct {
180178
skip int
181179
}
182180

181+
func (l Logger) clone() Logger {
182+
l.sinks = append([]sink(nil), l.sinks...)
183+
return l
184+
}
185+
183186
func (l Logger) Debug(ctx context.Context, msg string, fields ...Field) {
184187
l.testingHelper()
185188
l.log(ctx, LevelDebug, msg, fields)
@@ -211,15 +214,15 @@ func (l Logger) Fatal(ctx context.Context, msg string, fields ...Field) {
211214
}
212215

213216
func (l Logger) With(fields ...Field) Logger {
214-
sinks := make([]sink, len(l.sinks))
217+
l = l.clone()
215218
for i, s := range l.sinks {
216219
s.pl = s.pl.withFields(fields)
217-
sinks[i] = s
220+
l.sinks[i] = s
218221
}
219-
l.sinks = sinks
220222
return l
221223
}
222224

225+
// SetLevel changes the Logger's level.
223226
func (l Logger) SetLevel(level Level) {
224227
for _, s := range l.sinks {
225228
atomic.StoreInt64(s.level, int64(level))
@@ -358,3 +361,8 @@ func Tee(ls ...Logger) Logger {
358361

359362
return l
360363
}
364+
365+
type testSink interface {
366+
Stdlib() Sink
367+
TestingHelper() func()
368+
}

sloggers/slogtest/test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"go.coder.com/slog"
99
"go.coder.com/slog/internal/humanfmt"
10-
"go.coder.com/slog/internal/stdlibctx"
1110
)
1211

1312
// TestOptions represents the options for the logger returned
@@ -30,11 +29,23 @@ func Make(tb testing.TB, opts *TestOptions) slog.Logger {
3029
}
3130

3231
type testSink struct {
33-
tb testing.TB
34-
opts *TestOptions
32+
tb testing.TB
33+
opts *TestOptions
34+
stdlib bool
3535
}
3636

37-
func (ts testSink) XXX_slogTestingHelper() func() {
37+
func (ts testSink) Stdlib() slog.Sink {
38+
ts.stdlib = true
39+
return ts
40+
}
41+
42+
// Implements:
43+
// type testSink interface {
44+
// Stdlib() Sink
45+
// TestingHelper() func()
46+
// }
47+
// See slog.go in root package.
48+
func (ts testSink) TestingHelper() func() {
3849
return ts.tb.Helper
3950
}
4051

@@ -43,7 +54,7 @@ var stderrColor = humanfmt.IsTTY(os.Stderr)
4354
func (ts testSink) LogEntry(ctx context.Context, ent slog.Entry) {
4455
ts.tb.Helper()
4556

46-
if !stdlibctx.From(ctx) {
57+
if !ts.stdlib {
4758
// We do not want to print the file or line number ourselves.
4859
// The testing framework handles it for us.
4960
// But we do want the function name.

stdlib.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"log"
66
"strings"
7-
8-
"go.coder.com/slog/internal/stdlibctx"
97
)
108

119
// Stdlib creates a standard library logger from the given logger.
@@ -20,7 +18,12 @@ import (
2018
func Stdlib(ctx context.Context, l Logger) *log.Logger {
2119
l.skip += 4
2220

23-
ctx = stdlibctx.With(ctx)
21+
l = l.clone()
22+
for i, s := range l.sinks {
23+
if ts, ok := s.sink.(testSink); ok {
24+
l.sinks[i].sink = ts.Stdlib()
25+
}
26+
}
2427

2528
w := &stdlogWriter{
2629
Log: func(msg string) {

0 commit comments

Comments
 (0)