Skip to content

Commit

Permalink
unexport delegating logger
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@redhat.com>
  • Loading branch information
vincepri committed May 5, 2023
1 parent c03340f commit e7a87dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
34 changes: 17 additions & 17 deletions pkg/log/deleg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ import (
// loggerPromise knows how to populate a concrete logr.Logger
// with options, given an actual base logger later on down the line.
type loggerPromise struct {
delegating *DelegatingLogSink
delegating *delegatingLogSink
childPromises []*loggerPromise
promisesLock sync.Mutex

name *string
tags []interface{}
}

func (p *loggerPromise) WithName(l *DelegatingLogSink, name string) *loggerPromise {
func (p *loggerPromise) WithName(l *delegatingLogSink, name string) *loggerPromise {
res := &loggerPromise{
delegating: l,
name: &name,
Expand All @@ -51,7 +51,7 @@ func (p *loggerPromise) WithName(l *DelegatingLogSink, name string) *loggerPromi
}

// WithValues provides a new Logger with the tags appended.
func (p *loggerPromise) WithValues(l *DelegatingLogSink, tags ...interface{}) *loggerPromise {
func (p *loggerPromise) WithValues(l *delegatingLogSink, tags ...interface{}) *loggerPromise {
res := &loggerPromise{
delegating: l,
tags: tags,
Expand Down Expand Up @@ -88,12 +88,12 @@ func (p *loggerPromise) Fulfill(parentLogSink logr.LogSink) {
}
}

// DelegatingLogSink is a logsink that delegates to another logr.LogSink.
// delegatingLogSink is a logsink that delegates to another logr.LogSink.
// If the underlying promise is not nil, it registers calls to sub-loggers with
// the logging factory to be populated later, and returns a new delegating
// logger. It expects to have *some* logr.Logger set at all times (generally
// a no-op logger before the promises are fulfilled).
type DelegatingLogSink struct {
type delegatingLogSink struct {
eventuallyFulfill func()

info atomic.Pointer[logr.RuntimeInfo]
Expand All @@ -104,15 +104,15 @@ type DelegatingLogSink struct {
}

// Init implements logr.LogSink.
func (l *DelegatingLogSink) Init(info logr.RuntimeInfo) {
func (l *delegatingLogSink) Init(info logr.RuntimeInfo) {
l.eventuallyFulfill()
l.info.Store(&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 *DelegatingLogSink) Enabled(level int) bool {
func (l *delegatingLogSink) Enabled(level int) bool {
l.eventuallyFulfill()
return (*l.logger.Load()).Enabled(level)
}
Expand All @@ -123,7 +123,7 @@ func (l *DelegatingLogSink) Enabled(level int) 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 *DelegatingLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
func (l *delegatingLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
l.eventuallyFulfill()
(*l.logger.Load()).Info(level, msg, keysAndValues...)
}
Expand All @@ -136,18 +136,18 @@ func (l *DelegatingLogSink) Info(level int, msg string, keysAndValues ...interfa
// The msg field should be used to add context to any underlying error,
// while the err field should be used to attach the actual error that
// triggered this log line, if present.
func (l *DelegatingLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
func (l *delegatingLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
l.eventuallyFulfill()
(*l.logger.Load()).Error(err, msg, keysAndValues...)
}

// WithName provides a new Logger with the name appended.
func (l *DelegatingLogSink) WithName(name string) logr.LogSink {
func (l *delegatingLogSink) WithName(name string) logr.LogSink {
l.eventuallyFulfill()

switch {
case l.promise.Load() != nil:
res := &DelegatingLogSink{eventuallyFulfill: l.eventuallyFulfill}
res := &delegatingLogSink{eventuallyFulfill: l.eventuallyFulfill}
res.logger.Store(l.logger.Load())
promise := l.promise.Load().WithName(res, name)
res.promise.Store(promise)
Expand All @@ -162,12 +162,12 @@ func (l *DelegatingLogSink) WithName(name string) logr.LogSink {
}

// WithValues provides a new Logger with the tags appended.
func (l *DelegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
func (l *delegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
l.eventuallyFulfill()

switch {
case l.promise.Load() != nil:
res := &DelegatingLogSink{eventuallyFulfill: l.eventuallyFulfill}
res := &delegatingLogSink{eventuallyFulfill: l.eventuallyFulfill}
res.logger.Store(l.logger.Load())
promise := l.promise.Load().WithValues(res, tags...)
res.promise.Store(promise)
Expand All @@ -184,17 +184,17 @@ func (l *DelegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
// 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 *DelegatingLogSink) Fulfill(actual logr.LogSink) {
func (l *delegatingLogSink) Fulfill(actual logr.LogSink) {
if promise := l.promise.Load(); promise != nil {
promise.Fulfill(actual)
l.fulfilled.Store(true)
}
}

// NewDelegatingLogSink constructs a new DelegatingLogSink which uses
// newDelegatingLogSink constructs a new DelegatingLogSink which uses
// the given logger before its promise is fulfilled.
func NewDelegatingLogSink(initial logr.LogSink) *DelegatingLogSink {
root := &DelegatingLogSink{}
func newDelegatingLogSink(initial logr.LogSink) *delegatingLogSink {
root := &delegatingLogSink{}
root.logger.Store(&initial)
root.promise.Store(&loggerPromise{delegating: root})
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func SetLogger(l logr.Logger) {
// the first 30 seconds of a binaries lifetime, it will get
// set to a NullLogSink.
var (
dlog = NewDelegatingLogSink(NullLogSink{})
dlog = newDelegatingLogSink(NullLogSink{})
Log = logr.New(dlog)
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
. "github.com/onsi/gomega"
)

var _ logr.LogSink = &DelegatingLogSink{}
var _ logr.LogSink = &delegatingLogSink{}

// logInfo is the information for a particular fakeLogger message.
type logInfo struct {
Expand Down Expand Up @@ -124,13 +124,13 @@ var _ = Describe("logging", func() {
var (
root *fakeLoggerRoot
baseLog logr.LogSink
delegLog *DelegatingLogSink
delegLog *delegatingLogSink
)

BeforeEach(func() {
root = &fakeLoggerRoot{}
baseLog = &fakeLogger{root: root}
delegLog = NewDelegatingLogSink(NullLogSink{})
delegLog = newDelegatingLogSink(NullLogSink{})
})

It("should delegate with name", func() {
Expand Down

0 comments on commit e7a87dc

Please sign in to comment.