From 3dfe5be206b6038c05f94ccf16b054b55e97980e Mon Sep 17 00:00:00 2001 From: Morlay Date: Thu, 15 Jul 2021 18:12:06 +0800 Subject: [PATCH] Bump github.com/go-logr/zapr & github.com/go-logr/logr to v1.0.0 --- go.mod | 4 +-- go.sum | 9 ++++--- pkg/log/deleg.go | 66 +++++++++++++-------------------------------- pkg/log/log.go | 15 ++++++----- pkg/log/log_test.go | 46 ++++++++++++++++--------------- pkg/log/null.go | 28 ++++++++++--------- 6 files changed, 76 insertions(+), 92 deletions(-) diff --git a/go.mod b/go.mod index 0ce3b6c132..0ca5037c6a 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.16 require ( github.com/evanphx/json-patch v4.11.0+incompatible github.com/fsnotify/fsnotify v1.4.9 - github.com/go-logr/logr v0.4.0 - github.com/go-logr/zapr v0.4.0 + github.com/go-logr/logr v1.0.0 + github.com/go-logr/zapr v1.0.0 github.com/googleapis/gnostic v0.5.5 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/imdario/mergo v0.3.12 // indirect diff --git a/go.sum b/go.sum index 12cf1b35ba..e3dd5e90fe 100644 --- a/go.sum +++ b/go.sum @@ -111,10 +111,12 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/zapr v0.4.0 h1:uc1uML3hRYL9/ZZPdgHS/n8Nzo+eaYL/Efxkkamf7OM= -github.com/go-logr/zapr v0.4.0/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk= +github.com/go-logr/logr v1.0.0-rc1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= +github.com/go-logr/logr v1.0.0 h1:kH951GinvFVaQgy/ki/B3YYmQtRpExGigSJg6O8z5jo= +github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/zapr v1.0.0 h1:rerrgIsgykt8zVvKMVfqxI2SoYvHAFdX11er/SLZZgI= +github.com/go-logr/zapr v1.0.0/go.mod h1:t7rgfcj/l02iFgbQxqhQeoyWA9jX2+2enc4PUHF6Hp0= github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc= @@ -399,6 +401,7 @@ go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.8.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= diff --git a/pkg/log/deleg.go b/pkg/log/deleg.go index bbd9c9c756..3a1c1d244c 100644 --- a/pkg/log/deleg.go +++ b/pkg/log/deleg.go @@ -29,9 +29,8 @@ type loggerPromise struct { childPromises []*loggerPromise promisesLock sync.Mutex - name *string - tags []interface{} - level int + name *string + tags []interface{} } func (p *loggerPromise) WithName(l *DelegatingLogger, name string) *loggerPromise { @@ -61,21 +60,8 @@ func (p *loggerPromise) WithValues(l *DelegatingLogger, tags ...interface{}) *lo return res } -func (p *loggerPromise) V(l *DelegatingLogger, level int) *loggerPromise { - res := &loggerPromise{ - logger: l, - level: level, - promisesLock: sync.Mutex{}, - } - - p.promisesLock.Lock() - defer p.promisesLock.Unlock() - p.childPromises = append(p.childPromises, res) - return res -} - // Fulfill instantiates the Logger with the provided logger. -func (p *loggerPromise) Fulfill(parentLogger logr.Logger) { +func (p *loggerPromise) Fulfill(parentLogger logr.LogSink) { var logger = parentLogger if p.name != nil { logger = logger.WithName(*p.name) @@ -84,9 +70,6 @@ func (p *loggerPromise) Fulfill(parentLogger logr.Logger) { if p.tags != nil { logger = logger.WithValues(p.tags...) } - if p.level != 0 { - logger = logger.V(p.level) - } p.logger.lock.Lock() p.logger.logger = logger @@ -105,17 +88,23 @@ func (p *loggerPromise) Fulfill(parentLogger logr.Logger) { // a no-op logger before the promises are fulfilled). type DelegatingLogger struct { lock sync.RWMutex - logger logr.Logger + logger logr.LogSink promise *loggerPromise + info logr.RuntimeInfo +} + +// Init implements logr.LogSink. +func (l *DelegatingLogger) Init(info logr.RuntimeInfo) { + l.info = info } // Enabled tests whether this Logger is enabled. For example, commandline // flags might be used to set the logging verbosity and disable some info // logs. -func (l *DelegatingLogger) Enabled() bool { +func (l *DelegatingLogger) Enabled(v int) bool { l.lock.RLock() defer l.lock.RUnlock() - return l.logger.Enabled() + return l.logger.Enabled(v) } // Info logs a non-error message with the given key/value pairs as context. @@ -124,10 +113,10 @@ func (l *DelegatingLogger) Enabled() bool { // the log line. The key/value pairs can then be used to add additional // variable information. The key/value pairs should alternate string // keys and arbitrary values. -func (l *DelegatingLogger) Info(msg string, keysAndValues ...interface{}) { +func (l *DelegatingLogger) Info(level int, msg string, keysAndValues ...interface{}) { l.lock.RLock() defer l.lock.RUnlock() - l.logger.Info(msg, keysAndValues...) + l.logger.Info(level, msg, keysAndValues...) } // Error logs an error, with the given message and key/value pairs as context. @@ -144,27 +133,8 @@ func (l *DelegatingLogger) Error(err error, msg string, keysAndValues ...interfa l.logger.Error(err, msg, keysAndValues...) } -// V returns an Logger value for a specific verbosity level, relative to -// this Logger. In other words, V values are additive. V higher verbosity -// level means a log message is less important. It's illegal to pass a log -// level less than zero. -func (l *DelegatingLogger) V(level int) logr.Logger { - l.lock.RLock() - defer l.lock.RUnlock() - - if l.promise == nil { - return l.logger.V(level) - } - - res := &DelegatingLogger{logger: l.logger} - promise := l.promise.V(res, level) - res.promise = promise - - return res -} - // WithName provides a new Logger with the name appended. -func (l *DelegatingLogger) WithName(name string) logr.Logger { +func (l *DelegatingLogger) WithName(name string) logr.LogSink { l.lock.RLock() defer l.lock.RUnlock() @@ -180,7 +150,7 @@ func (l *DelegatingLogger) WithName(name string) logr.Logger { } // WithValues provides a new Logger with the tags appended. -func (l *DelegatingLogger) WithValues(tags ...interface{}) logr.Logger { +func (l *DelegatingLogger) WithValues(tags ...interface{}) logr.LogSink { l.lock.RLock() defer l.lock.RUnlock() @@ -198,7 +168,7 @@ func (l *DelegatingLogger) WithValues(tags ...interface{}) logr.Logger { // Fulfill switches the logger over to use the actual logger // provided, instead of the temporary initial one, if this method // has not been previously called. -func (l *DelegatingLogger) Fulfill(actual logr.Logger) { +func (l *DelegatingLogger) Fulfill(actual logr.LogSink) { if l.promise != nil { l.promise.Fulfill(actual) } @@ -206,7 +176,7 @@ func (l *DelegatingLogger) Fulfill(actual logr.Logger) { // NewDelegatingLogger constructs a new DelegatingLogger which uses // the given logger before it's promise is fulfilled. -func NewDelegatingLogger(initial logr.Logger) *DelegatingLogger { +func NewDelegatingLogger(initial logr.LogSink) *DelegatingLogger { l := &DelegatingLogger{ logger: initial, promise: &loggerPromise{promisesLock: sync.Mutex{}}, diff --git a/pkg/log/log.go b/pkg/log/log.go index 229ac7ec35..953b0c3a05 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -42,12 +42,12 @@ import ( ) // SetLogger sets a concrete logging implementation for all deferred Loggers. -func SetLogger(l logr.Logger) { +func SetLogger(l logr.LogSink) { loggerWasSetLock.Lock() defer loggerWasSetLock.Unlock() loggerWasSet = true - Log.Fulfill(l) + dlog.Fulfill(l) } // It is safe to assume that if this wasn't set within the first 30 seconds of a binaries @@ -64,7 +64,7 @@ func init() { loggerWasSetLock.Lock() defer loggerWasSetLock.Unlock() if !loggerWasSet { - Log.Fulfill(NullLogger{}) + dlog.Fulfill(NullLogger{}) } }() } @@ -79,13 +79,16 @@ var ( // get any actual logging. If SetLogger is not called within // the first 30 seconds of a binaries lifetime, it will get // set to a NullLogger. -var Log = NewDelegatingLogger(NullLogger{}) +var ( + dlog = NewDelegatingLogger(NullLogger{}) + Log = logr.New(dlog) +) // FromContext returns a logger with predefined values from a context.Context. func FromContext(ctx context.Context, keysAndValues ...interface{}) logr.Logger { - var log logr.Logger = Log + log := Log if ctx != nil { - if logger := logr.FromContext(ctx); logger != nil { + if logger, err := logr.FromContext(ctx); err == nil { log = logger } } diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index be3ad87997..e80cbaf12f 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -25,7 +25,7 @@ import ( . "github.com/onsi/gomega" ) -var _ logr.Logger = &DelegatingLogger{} +var _ logr.LogSink = &DelegatingLogger{} // logInfo is the information for a particular fakeLogger message. type logInfo struct { @@ -49,7 +49,10 @@ type fakeLogger struct { root *fakeLoggerRoot } -func (f *fakeLogger) WithName(name string) logr.Logger { +func (f *fakeLogger) Init(info logr.RuntimeInfo) { +} + +func (f *fakeLogger) WithName(name string) logr.LogSink { names := append([]string(nil), f.name...) names = append(names, name) return &fakeLogger{ @@ -59,7 +62,7 @@ func (f *fakeLogger) WithName(name string) logr.Logger { } } -func (f *fakeLogger) WithValues(vals ...interface{}) logr.Logger { +func (f *fakeLogger) WithValues(vals ...interface{}) logr.LogSink { tags := append([]interface{}(nil), f.tags...) tags = append(tags, vals...) return &fakeLogger{ @@ -80,7 +83,7 @@ func (f *fakeLogger) Error(err error, msg string, vals ...interface{}) { }) } -func (f *fakeLogger) Info(msg string, vals ...interface{}) { +func (f *fakeLogger) Info(l int, msg string, vals ...interface{}) { tags := append([]interface{}(nil), f.tags...) tags = append(tags, vals...) f.root.messages = append(f.root.messages, logInfo{ @@ -90,8 +93,9 @@ func (f *fakeLogger) Info(msg string, vals ...interface{}) { }) } -func (f *fakeLogger) Enabled() bool { return true } -func (f *fakeLogger) V(lvl int) logr.Logger { return f } +func (f *fakeLogger) Enabled(l int) bool { + return true +} var _ = Describe("logging", func() { @@ -121,7 +125,7 @@ var _ = Describe("logging", func() { Describe("lazy logger initialization", func() { var ( root *fakeLoggerRoot - baseLog logr.Logger + baseLog logr.LogSink delegLog *DelegatingLogger ) @@ -133,12 +137,12 @@ var _ = Describe("logging", func() { It("should delegate with name", func() { By("asking for a logger with a name before fulfill, and logging") - befFulfill1 := delegLog.WithName("before-fulfill") + befFulfill1 := logr.New(delegLog).WithName("before-fulfill") befFulfill2 := befFulfill1.WithName("two") befFulfill1.Info("before fulfill") By("logging on the base logger before fulfill") - delegLog.Info("before fulfill base") + logr.New(delegLog).Info("before fulfill base") By("ensuring that no messages were actually recorded") Expect(root.messages).To(BeEmpty()) @@ -154,7 +158,7 @@ var _ = Describe("logging", func() { befFulfill1.WithName("after-from-before").Info("after 3") By("logging with new loggers") - delegLog.WithName("after-fulfill").Info("after 4") + logr.New(delegLog).WithName("after-fulfill").Info("after 4") By("ensuring that the messages are appropriately named") Expect(root.messages).To(ConsistOf( @@ -179,7 +183,7 @@ var _ = Describe("logging", func() { // Constructing the child in the goroutine does not reliably // trigger the race detector - child := delegLog.WithName("child") + child := logr.New(delegLog).WithName("child") go func() { defer GinkgoRecover() delegLog.Fulfill(NullLogger{}) @@ -202,12 +206,12 @@ var _ = Describe("logging", func() { }() go func() { defer GinkgoRecover() - delegLog.Enabled() + logr.New(delegLog).Enabled() close(logEnabledDone) }() go func() { defer GinkgoRecover() - delegLog.Info("hello world") + logr.New(delegLog).Info("hello world") close(logInfoDone) }() go func() { @@ -217,7 +221,7 @@ var _ = Describe("logging", func() { }() go func() { defer GinkgoRecover() - delegLog.V(1) + logr.New(delegLog).V(1) close(logVDone) }() @@ -233,12 +237,12 @@ var _ = Describe("logging", func() { It("should delegate with tags", func() { By("asking for a logger with a name before fulfill, and logging") - befFulfill1 := delegLog.WithValues("tag1", "val1") + befFulfill1 := logr.New(delegLog).WithValues("tag1", "val1") befFulfill2 := befFulfill1.WithValues("tag2", "val2") befFulfill1.Info("before fulfill") By("logging on the base logger before fulfill") - delegLog.Info("before fulfill base") + logr.New(delegLog).Info("before fulfill base") By("ensuring that no messages were actually recorded") Expect(root.messages).To(BeEmpty()) @@ -254,7 +258,7 @@ var _ = Describe("logging", func() { befFulfill1.WithValues("tag3", "val3").Info("after 3") By("logging with new loggers") - delegLog.WithValues("tag3", "val3").Info("after 4") + logr.New(delegLog).WithValues("tag3", "val3").Info("after 4") By("ensuring that the messages are appropriately named") Expect(root.messages).To(ConsistOf( @@ -270,13 +274,13 @@ var _ = Describe("logging", func() { delegLog.Fulfill(baseLog) By("logging a bit") - delegLog.Info("msg 1") + logr.New(delegLog).Info("msg 1") By("fulfilling with a new logger") delegLog.Fulfill(&fakeLogger{}) By("logging some more") - delegLog.Info("msg 2") + logr.New(delegLog).Info("msg 2") By("checking that all log messages are present") Expect(root.messages).To(ConsistOf( @@ -296,7 +300,7 @@ var _ = Describe("logging", func() { root := &fakeLoggerRoot{} baseLog := &fakeLogger{root: root} - wantLog := baseLog.WithName("my-logger") + wantLog := logr.New(baseLog).WithName("my-logger") ctx := IntoContext(context.Background(), wantLog) gotLog := FromContext(ctx) @@ -312,7 +316,7 @@ var _ = Describe("logging", func() { root := &fakeLoggerRoot{} baseLog := &fakeLogger{root: root} - wantLog := baseLog.WithName("my-logger") + wantLog := logr.New(baseLog).WithName("my-logger") ctx := IntoContext(context.Background(), wantLog) gotLog := FromContext(ctx, "tag1", "value1") diff --git a/pkg/log/null.go b/pkg/log/null.go index 09a5a02eb6..f534fbcfa2 100644 --- a/pkg/log/null.go +++ b/pkg/log/null.go @@ -27,34 +27,38 @@ import ( // NullLogger is a logr.Logger that does nothing. type NullLogger struct{} -var _ logr.Logger = NullLogger{} +var _ logr.LogSink = NullLogger{} -// Info implements logr.InfoLogger. -func (NullLogger) Info(_ string, _ ...interface{}) { +// Init implements logr.LogSink. +func (log NullLogger) Init(_ logr.RuntimeInfo) { +} + +// Info implements logr.LogSink. +func (NullLogger) Info(_ int, _ string, _ ...interface{}) { // Do nothing. } -// Enabled implements logr.InfoLogger. -func (NullLogger) Enabled() bool { +// Enabled implements logr.LogSink. +func (NullLogger) Enabled(_ int) bool { return false } -// Error implements logr.Logger. +// Error implements logr.LogSink. func (NullLogger) Error(_ error, _ string, _ ...interface{}) { // Do nothing. } -// V implements logr.Logger. -func (log NullLogger) V(_ int) logr.Logger { +// V implements logr.LogSink. +func (log NullLogger) V(_ int) logr.LogSink { return log } -// WithName implements logr.Logger. -func (log NullLogger) WithName(_ string) logr.Logger { +// WithName implements logr.LogSink. +func (log NullLogger) WithName(_ string) logr.LogSink { return log } -// WithValues implements logr.Logger. -func (log NullLogger) WithValues(_ ...interface{}) logr.Logger { +// WithValues implements logr.LogSink. +func (log NullLogger) WithValues(_ ...interface{}) logr.LogSink { return log }