-
Notifications
You must be signed in to change notification settings - Fork 79
/
testimpls_slog_test.go
140 lines (114 loc) · 3.79 KB
/
testimpls_slog_test.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//go:build go1.21
// +build go1.21
/*
Copyright 2023 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logr
import (
"context"
"log/slog"
"time"
)
var _ SlogSink = &testSlogSink{}
// testSlogSink is a trivial SlogSink implementation, just for testing, which
// calls (optional) hooks on each method.
type testSlogSink struct {
// embed a plain LogSink
testLogSink
attrs []slog.Attr
groups []string
fnHandle func(ss *testSlogSink, ctx context.Context, record slog.Record)
fnWithAttrs func(ss *testSlogSink, attrs []slog.Attr)
fnWithGroup func(ss *testSlogSink, name string)
}
func (ss *testSlogSink) Handle(ctx context.Context, record slog.Record) error {
if ss.fnHandle != nil {
ss.fnHandle(ss, ctx, record)
}
return nil
}
func (ss *testSlogSink) WithAttrs(attrs []slog.Attr) SlogSink {
if ss.fnWithAttrs != nil {
ss.fnWithAttrs(ss, attrs)
}
out := *ss
n := len(out.attrs)
out.attrs = append(out.attrs[:n:n], attrs...)
return &out
}
func (ss *testSlogSink) WithGroup(name string) SlogSink {
if ss.fnWithGroup != nil {
ss.fnWithGroup(ss, name)
}
out := *ss
n := len(out.groups)
out.groups = append(out.groups[:n:n], name)
return &out
}
// passthruLogSink is a trivial LogSink implementation, which implements the
// logr.LogSink methods in terms of a slog.Handler.
type passthruLogSink struct {
handler slog.Handler
}
func (pl passthruLogSink) Init(RuntimeInfo) {}
func (pl passthruLogSink) Enabled(int) bool { return true }
func (pl passthruLogSink) Error(_ error, msg string, kvList ...interface{}) {
var record slog.Record
record.Message = msg
record.Level = slog.LevelError
record.Time = time.Now()
record.Add(kvList...)
_ = pl.handler.Handle(context.Background(), record)
}
func (pl passthruLogSink) Info(_ int, msg string, kvList ...interface{}) {
var record slog.Record
record.Message = msg
record.Level = slog.LevelInfo
record.Time = time.Now()
record.Add(kvList...)
_ = pl.handler.Handle(context.Background(), record)
}
func (pl passthruLogSink) WithName(string) LogSink { return &pl }
func (pl passthruLogSink) WithValues(kvList ...interface{}) LogSink {
var values slog.Record
values.Add(kvList...)
var attrs []slog.Attr
add := func(attr slog.Attr) bool {
attrs = append(attrs, attr)
return true
}
values.Attrs(add)
pl.handler = pl.handler.WithAttrs(attrs)
return &pl
}
// passthruSlogSink is a trivial SlogSink implementation, which stubs out the
// logr.LogSink methods and passes Logr.SlogSink thru to a slog.Handler.
type passthruSlogSink struct {
handler slog.Handler
}
func (ps passthruSlogSink) Init(RuntimeInfo) {}
func (ps passthruSlogSink) Enabled(int) bool { return true }
func (ps passthruSlogSink) Error(error, string, ...interface{}) {}
func (ps passthruSlogSink) Info(int, string, ...interface{}) {}
func (ps passthruSlogSink) WithName(string) LogSink { return &ps }
func (ps passthruSlogSink) WithValues(...interface{}) LogSink { return &ps }
func (ps *passthruSlogSink) Handle(ctx context.Context, record slog.Record) error {
return ps.handler.Handle(ctx, record)
}
func (ps passthruSlogSink) WithAttrs(attrs []slog.Attr) SlogSink {
ps.handler = ps.handler.WithAttrs(attrs)
return &ps
}
func (ps passthruSlogSink) WithGroup(name string) SlogSink {
ps.handler = ps.handler.WithGroup(name)
return &ps
}