Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The logger was previously available on the Config object as well as from the context via `logr.FromContext(ctx)`. While they are the same root logger, each has a different set of names and values applied to each log line. Loading the logger from the context is the preferred approach moving forward. The logger on the Config is still available, but is deprecated and will be removed in a future release. The first use of the `c.Log` methods will trigger an error printed to the log. Each reconciler in the call chain can enrich the logger as it is passed deeper in the reconciler hierarchy. Many reconciler have a new `Name` field that is appended to the logger's name for all log statements under it. `ParentReconciler`, `ChildReconciler` and `CastParent` provide default names based on the type they are reconciling. Unique names are recommended, but are not required, since they only appear in logs and do not affect the runtime behavior. Signed-off-by: Scott Andrews <andrewssc@vmware.com>
- Loading branch information
Showing
6 changed files
with
218 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2022 VMware, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package reconcilers | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
var ( | ||
_ logr.LogSink = (*warnOnceLogSink)(nil) | ||
) | ||
|
||
// Deprecated | ||
func newWarnOnceLogger(log logr.Logger) logr.Logger { | ||
return logr.New(&warnOnceLogSink{ | ||
sink: log.GetSink(), | ||
}) | ||
} | ||
|
||
// Deprecated | ||
type warnOnceLogSink struct { | ||
sink logr.LogSink | ||
once sync.Once | ||
} | ||
|
||
func (s *warnOnceLogSink) Init(info logr.RuntimeInfo) { | ||
s.sink.Init(info) | ||
} | ||
|
||
func (s *warnOnceLogSink) Enabled(level int) bool { | ||
return s.sink.Enabled(level) | ||
} | ||
|
||
func (s *warnOnceLogSink) Info(level int, msg string, keysAndValues ...interface{}) { | ||
s.warn() | ||
s.sink.Info(level, msg, keysAndValues...) | ||
} | ||
|
||
func (s *warnOnceLogSink) Error(err error, msg string, keysAndValues ...interface{}) { | ||
s.warn() | ||
s.sink.Error(err, msg, keysAndValues...) | ||
} | ||
|
||
func (s *warnOnceLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink { | ||
return &warnOnceLogSink{ | ||
sink: s.sink.WithValues(keysAndValues...), | ||
once: s.once, | ||
} | ||
} | ||
|
||
func (s *warnOnceLogSink) WithName(name string) logr.LogSink { | ||
return &warnOnceLogSink{ | ||
sink: s.sink.WithName(name), | ||
once: s.once, | ||
} | ||
} | ||
|
||
func (s *warnOnceLogSink) warn() { | ||
s.once.Do(func() { | ||
s.sink.Error(fmt.Errorf("Config.Log is deprecated"), "use a logger from the context: `log := logr.FromContext(ctx)`") | ||
}) | ||
} |
Oops, something went wrong.