...
1
16
17
18 package testr
19
20 import (
21 "testing"
22
23 "github.com/go-logr/logr"
24 "github.com/go-logr/logr/funcr"
25 )
26
27
28
29 func New(t *testing.T) logr.Logger {
30 return NewWithOptions(t, Options{})
31 }
32
33
34 type Options struct {
35
36
37
38 LogTimestamp bool
39
40
41
42 Verbosity int
43 }
44
45
46
47 func NewWithOptions(t *testing.T, opts Options) logr.Logger {
48 l := &testlogger{
49 testloggerInterface: newLoggerInterfaceWithOptions(t, opts),
50 }
51 return logr.New(l)
52 }
53
54
55 type TestingT interface {
56 Helper()
57 Log(args ...any)
58 }
59
60
61
62
63 func NewWithInterface(t TestingT, opts Options) logr.Logger {
64 l := newLoggerInterfaceWithOptions(t, opts)
65 return logr.New(&l)
66 }
67
68 func newLoggerInterfaceWithOptions(t TestingT, opts Options) testloggerInterface {
69 return testloggerInterface{
70 t: t,
71 Formatter: funcr.NewFormatter(funcr.Options{
72 LogTimestamp: opts.LogTimestamp,
73 Verbosity: opts.Verbosity,
74 }),
75 }
76 }
77
78
79
80
81
82 type Underlier interface {
83 GetUnderlying() *testing.T
84 }
85
86
87
88
89
90 type UnderlierInterface interface {
91 GetUnderlying() TestingT
92 }
93
94
95 func logInfo(t TestingT, formatInfo func(int, string, []any) (string, string), level int, msg string, kvList ...any) {
96 prefix, args := formatInfo(level, msg, kvList)
97 t.Helper()
98 if prefix != "" {
99 args = prefix + ": " + args
100 }
101 t.Log(args)
102 }
103
104
105 func logError(t TestingT, formatError func(error, string, []any) (string, string), err error, msg string, kvList ...any) {
106 prefix, args := formatError(err, msg, kvList)
107 t.Helper()
108 if prefix != "" {
109 args = prefix + ": " + args
110 }
111 t.Log(args)
112 }
113
114
115
116 type testlogger struct {
117 testloggerInterface
118 }
119
120 func (l testlogger) GetUnderlying() *testing.T {
121
122
123
124 return l.t.(*testing.T)
125 }
126
127 type testloggerInterface struct {
128 funcr.Formatter
129 t TestingT
130 }
131
132 func (l testloggerInterface) WithName(name string) logr.LogSink {
133 l.Formatter.AddName(name)
134 return &l
135 }
136
137 func (l testloggerInterface) WithValues(kvList ...any) logr.LogSink {
138 l.Formatter.AddValues(kvList)
139 return &l
140 }
141
142 func (l testloggerInterface) GetCallStackHelper() func() {
143 return l.t.Helper
144 }
145
146 func (l testloggerInterface) Info(level int, msg string, kvList ...any) {
147 l.t.Helper()
148 logInfo(l.t, l.FormatInfo, level, msg, kvList...)
149 }
150
151 func (l testloggerInterface) Error(err error, msg string, kvList ...any) {
152 l.t.Helper()
153 logError(l.t, l.FormatError, err, msg, kvList...)
154 }
155
156 func (l testloggerInterface) GetUnderlying() TestingT {
157 return l.t
158 }
159
160
161 var _ logr.LogSink = &testlogger{}
162 var _ logr.CallStackHelperLogSink = &testlogger{}
163 var _ Underlier = &testlogger{}
164
165 var _ logr.LogSink = &testloggerInterface{}
166 var _ logr.CallStackHelperLogSink = &testloggerInterface{}
167 var _ UnderlierInterface = &testloggerInterface{}
168
View as plain text