Codebase list golang-github-go-logr-logr / ec9793c1-4cb7-4689-9df5-39e2532c4fbe/v0.4.0
Merge pull request #36 from thockin/eol-null-logger Effectively EOL testing.NullLogger Tim Hockin authored 3 years ago GitHub committed 3 years ago
5 changed file(s) with 22 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
1818 // Discard returns a valid Logger that discards all messages logged to it.
1919 // It can be used whenever the caller is not interested in the logs.
2020 func Discard() Logger {
21 return discardLogger{}
21 return DiscardLogger{}
2222 }
2323
24 // discardLogger is a Logger that discards all messages.
25 type discardLogger struct{}
24 // DiscardLogger is a Logger that discards all messages.
25 type DiscardLogger struct{}
2626
27 func (l discardLogger) Enabled() bool {
27 func (l DiscardLogger) Enabled() bool {
2828 return false
2929 }
3030
31 func (l discardLogger) Info(msg string, keysAndValues ...interface{}) {
31 func (l DiscardLogger) Info(msg string, keysAndValues ...interface{}) {
3232 }
3333
34 func (l discardLogger) Error(err error, msg string, keysAndValues ...interface{}) {
34 func (l DiscardLogger) Error(err error, msg string, keysAndValues ...interface{}) {
3535 }
3636
37 func (l discardLogger) V(level int) Logger {
37 func (l DiscardLogger) V(level int) Logger {
3838 return l
3939 }
4040
41 func (l discardLogger) WithValues(keysAndValues ...interface{}) Logger {
41 func (l DiscardLogger) WithValues(keysAndValues ...interface{}) Logger {
4242 return l
4343 }
4444
45 func (l discardLogger) WithName(name string) Logger {
45 func (l DiscardLogger) WithName(name string) Logger {
4646 return l
4747 }
4848
4949 // Verify that it actually implements the interface
50 var _ Logger = discardLogger{}
50 var _ Logger = DiscardLogger{}
2222
2323 func TestDiscard(t *testing.T) {
2424 l := Discard()
25 if _, ok := l.(discardLogger); !ok {
25 if _, ok := l.(DiscardLogger); !ok {
2626 t.Error("did not return the expected underlying type")
2727 }
2828 // Verify that none of the methods panic, there is not more we can test.
189189 // InfoLogger provides compatibility with code that relies on the v0.1.0
190190 // interface.
191191 //
192 // Deprecated: use Logger instead. This will be removed in a future release.
192 // Deprecated: InfoLogger is an artifact of early versions of this API. New
193 // users should never use it and existing users should use Logger instead. This
194 // will be removed in a future release.
193195 type InfoLogger = Logger
194196
195197 type contextKey struct{}
211213 return v
212214 }
213215
214 return discardLogger{}
216 return Discard()
215217 }
216218
217219 // NewContext returns a new context derived from ctx that embeds the Logger.
5656 }
5757 if out := FromContextOrDiscard(ctx); out == nil {
5858 t.Errorf("expected non-nil logger")
59 } else if _, ok := out.(discardLogger); !ok {
60 t.Errorf("expected a discardLogger, got %#v", out)
59 } else if _, ok := out.(DiscardLogger); !ok {
60 t.Errorf("expected a DiscardLogger, got %#v", out)
6161 }
6262
6363 logger := &testLogger{}
1818 import "github.com/go-logr/logr"
1919
2020 // NullLogger is a logr.Logger that does nothing.
21 type NullLogger struct{}
22
23 var _ logr.Logger = NullLogger{}
24
25 func (_ NullLogger) Info(_ string, _ ...interface{}) {
26 // Do nothing.
27 }
28
29 func (_ NullLogger) Enabled() bool {
30 return false
31 }
32
33 func (_ NullLogger) Error(_ error, _ string, _ ...interface{}) {
34 // Do nothing.
35 }
36
37 func (log NullLogger) V(_ int) logr.Logger {
38 return log
39 }
40
41 func (log NullLogger) WithName(_ string) logr.Logger {
42 return log
43 }
44
45 func (log NullLogger) WithValues(_ ...interface{}) logr.Logger {
46 return log
47 }
21 //
22 // Deprecated: NullLogger is idenitcal to logr.DiscardLogger. It is retained
23 // for backwards compatibility, but new users should use logr.DiscardLogger
24 // instead.
25 type NullLogger = logr.DiscardLogger