Skip to content

Commit 98ee9d9

Browse files
committed
Clean up slog testing and restore coverage
1 parent b228ba8 commit 98ee9d9

7 files changed

+352
-334
lines changed

logr_noslog_test.go

-23
This file was deleted.

logr_slog_test.go

-218
This file was deleted.

logr_test.go

-79
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,6 @@ import (
2424
"testing"
2525
)
2626

27-
// testLogSink is a Logger just for testing that calls optional hooks on each method.
28-
type testLogSink struct {
29-
fnInit func(ri RuntimeInfo)
30-
fnEnabled func(lvl int) bool
31-
fnInfo func(lvl int, msg string, kv ...any)
32-
fnError func(err error, msg string, kv ...any)
33-
fnWithValues func(kv ...any)
34-
fnWithName func(name string)
35-
36-
withValues []any
37-
38-
// testSlogSink contains some additional fields if (and only if) slog is supported by Go.
39-
// See logr_slog_test.go.
40-
//nolint:unused // Only unused with Go < 1.21.
41-
testSlogSink
42-
}
43-
44-
var _ LogSink = &testLogSink{}
45-
46-
func (l *testLogSink) Init(ri RuntimeInfo) {
47-
if l.fnInit != nil {
48-
l.fnInit(ri)
49-
}
50-
}
51-
52-
func (l *testLogSink) Enabled(lvl int) bool {
53-
if l.fnEnabled != nil {
54-
return l.fnEnabled(lvl)
55-
}
56-
return false
57-
}
58-
59-
func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
60-
if l.fnInfo != nil {
61-
l.fnInfo(lvl, msg, kv...)
62-
}
63-
}
64-
65-
func (l *testLogSink) Error(err error, msg string, kv ...any) {
66-
if l.fnError != nil {
67-
l.fnError(err, msg, kv...)
68-
}
69-
}
70-
71-
func (l *testLogSink) WithValues(kv ...any) LogSink {
72-
if l.fnWithValues != nil {
73-
l.fnWithValues(kv...)
74-
}
75-
out := *l
76-
n := len(out.withValues)
77-
out.withValues = append(out.withValues[:n:n], kv...)
78-
return &out
79-
}
80-
81-
func (l *testLogSink) WithName(name string) LogSink {
82-
if l.fnWithName != nil {
83-
l.fnWithName(name)
84-
}
85-
out := *l
86-
return &out
87-
}
88-
89-
type testCallDepthLogSink struct {
90-
testLogSink
91-
callDepth int
92-
fnWithCallDepth func(depth int)
93-
}
94-
95-
var _ CallDepthLogSink = &testCallDepthLogSink{}
96-
97-
func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
98-
if l.fnWithCallDepth != nil {
99-
l.fnWithCallDepth(depth)
100-
}
101-
out := *l
102-
out.callDepth += depth
103-
return &out
104-
}
105-
10627
func TestNew(t *testing.T) {
10728
calledInit := 0
10829

sloghandler.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ func (l *slogHandler) Handle(ctx context.Context, record slog.Record) error {
7070

7171
kvList := make([]any, 0, 2*record.NumAttrs())
7272
record.Attrs(func(attr slog.Attr) bool {
73-
if attr.Key != "" {
74-
kvList = append(kvList, l.addGroupPrefix(attr.Key), attr.Value.Resolve().Any())
75-
}
73+
kvList = attrToKVs(attr, l.groupPrefix, kvList)
7674
return true
7775
})
7876
if record.Level >= slog.LevelError {
@@ -114,9 +112,7 @@ func (l *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
114112
} else {
115113
kvList := make([]any, 0, 2*len(attrs))
116114
for _, attr := range attrs {
117-
if attr.Key != "" {
118-
kvList = append(kvList, l.addGroupPrefix(attr.Key), attr.Value.Resolve().Any())
119-
}
115+
kvList = attrToKVs(attr, l.groupPrefix, kvList)
120116
}
121117
clone.sink = l.sink.WithValues(kvList...)
122118
}
@@ -136,16 +132,41 @@ func (l *slogHandler) WithGroup(name string) slog.Handler {
136132
clone.slogSink = l.slogSink.WithGroup(name)
137133
clone.sink = clone.slogSink
138134
} else {
139-
clone.groupPrefix = clone.addGroupPrefix(name)
135+
clone.groupPrefix = addPrefix(clone.groupPrefix, name)
140136
}
141137
return &clone
142138
}
143139

144-
func (l *slogHandler) addGroupPrefix(name string) string {
145-
if l.groupPrefix == "" {
140+
// attrToKVs appends a slog.Attr to a logr-style kvList. It handle slog Groups
141+
// and other details of slog.
142+
func attrToKVs(attr slog.Attr, groupPrefix string, kvList []any) []any {
143+
attrVal := attr.Value.Resolve()
144+
if attrVal.Kind() == slog.KindGroup {
145+
groupVal := attrVal.Group()
146+
grpKVs := make([]any, 0, 2*len(groupVal))
147+
prefix := groupPrefix
148+
if attr.Key != "" {
149+
prefix = addPrefix(groupPrefix, attr.Key)
150+
}
151+
for _, attr := range groupVal {
152+
grpKVs = attrToKVs(attr, prefix, grpKVs)
153+
}
154+
kvList = append(kvList, grpKVs...)
155+
} else if attr.Key != "" {
156+
kvList = append(kvList, addPrefix(groupPrefix, attr.Key), attrVal.Any())
157+
}
158+
159+
return kvList
160+
}
161+
162+
func addPrefix(prefix, name string) string {
163+
if prefix == "" {
146164
return name
147165
}
148-
return l.groupPrefix + groupSeparator + name
166+
if name == "" {
167+
return prefix
168+
}
169+
return prefix + groupSeparator + name
149170
}
150171

151172
// levelFromSlog adjusts the level by the logger's verbosity and negates it.

0 commit comments

Comments
 (0)