@@ -27,11 +27,7 @@ import (
27
27
// New returns a logr.Logger that prints through a testing.T object.
28
28
// Info logs are only enabled at V(0).
29
29
func New (t * testing.T ) logr.Logger {
30
- l := & testlogger {
31
- Formatter : funcr .NewFormatter (funcr.Options {}),
32
- t : t ,
33
- }
34
- return logr .New (l )
30
+ return NewWithOptions (t , Options {})
35
31
}
36
32
37
33
// Options carries parameters which influence the way logs are generated.
@@ -50,11 +46,7 @@ type Options struct {
50
46
// In contrast to the simpler New, output formatting can be configured.
51
47
func NewWithOptions (t * testing.T , opts Options ) logr.Logger {
52
48
l := & testlogger {
53
- Formatter : funcr .NewFormatter (funcr.Options {
54
- LogTimestamp : opts .LogTimestamp ,
55
- Verbosity : opts .Verbosity ,
56
- }),
57
- t : t ,
49
+ testloggerInterface : newLoggerInterfaceWithOptions (t , opts ),
58
50
}
59
51
return logr .New (l )
60
52
}
@@ -69,14 +61,18 @@ type TestingT interface {
69
61
// TestingT object.
70
62
// In contrast to the simpler New, output formatting can be configured.
71
63
func NewWithInterface (t TestingT , opts Options ) logr.Logger {
72
- l := & testloggerInterface {
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 ,
73
71
Formatter : funcr .NewFormatter (funcr.Options {
74
72
LogTimestamp : opts .LogTimestamp ,
75
73
Verbosity : opts .Verbosity ,
76
74
}),
77
- t : t ,
78
75
}
79
- return logr .New (l )
80
76
}
81
77
82
78
// Underlier exposes access to the underlying testing.T instance. Since
@@ -115,37 +111,17 @@ func logError(t TestingT, formatError func(error, string, []interface{}) (string
115
111
t .Log (args )
116
112
}
117
113
114
+ // This type exists to wrap and modify the method-set of testloggerInterface.
115
+ // In particular, it changes the GetUnderlying() method.
118
116
type testlogger struct {
119
- funcr.Formatter
120
- t * testing.T
121
- }
122
-
123
- func (l testlogger ) WithName (name string ) logr.LogSink {
124
- l .Formatter .AddName (name )
125
- return & l
126
- }
127
-
128
- func (l testlogger ) WithValues (kvList ... interface {}) logr.LogSink {
129
- l .Formatter .AddValues (kvList )
130
- return & l
131
- }
132
-
133
- func (l testlogger ) GetCallStackHelper () func () {
134
- return l .t .Helper
135
- }
136
-
137
- func (l testlogger ) Info (level int , msg string , kvList ... interface {}) {
138
- l .t .Helper ()
139
- logInfo (l .t , l .FormatInfo , level , msg , kvList ... )
140
- }
141
-
142
- func (l testlogger ) Error (err error , msg string , kvList ... interface {}) {
143
- l .t .Helper ()
144
- logError (l .t , l .FormatError , err , msg , kvList ... )
117
+ testloggerInterface
145
118
}
146
119
147
120
func (l testlogger ) GetUnderlying () * testing.T {
148
- return l .t
121
+ // This method is defined on testlogger, so the only type this could
122
+ // possibly be is testing.T, even though that's not guaranteed by the type
123
+ // system itself.
124
+ return l .t .(* testing.T ) //nolint:forcetypeassert
149
125
}
150
126
151
127
type testloggerInterface struct {
0 commit comments