2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// loggerPromise knows how to populate a concrete logr.Logger
|
|
|
|
// with options, given an actual base logger later on down the line.
|
|
|
|
type loggerPromise struct {
|
2023-06-01 17:01:19 +00:00
|
|
|
logger *delegatingLogSink
|
2020-10-21 05:49:41 +00:00
|
|
|
childPromises []*loggerPromise
|
|
|
|
promisesLock sync.Mutex
|
|
|
|
|
2021-12-08 13:50:47 +00:00
|
|
|
name *string
|
|
|
|
tags []interface{}
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
func (p *loggerPromise) WithName(l *delegatingLogSink, name string) *loggerPromise {
|
2020-10-21 05:49:41 +00:00
|
|
|
res := &loggerPromise{
|
|
|
|
logger: l,
|
|
|
|
name: &name,
|
|
|
|
promisesLock: sync.Mutex{},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.promisesLock.Lock()
|
|
|
|
defer p.promisesLock.Unlock()
|
|
|
|
p.childPromises = append(p.childPromises, res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// WithValues provides a new Logger with the tags appended.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (p *loggerPromise) WithValues(l *delegatingLogSink, tags ...interface{}) *loggerPromise {
|
2020-10-21 05:49:41 +00:00
|
|
|
res := &loggerPromise{
|
|
|
|
logger: l,
|
|
|
|
tags: tags,
|
|
|
|
promisesLock: sync.Mutex{},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.promisesLock.Lock()
|
|
|
|
defer p.promisesLock.Unlock()
|
|
|
|
p.childPromises = append(p.childPromises, res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Fulfill instantiates the Logger with the provided logger.
|
2021-12-08 13:50:47 +00:00
|
|
|
func (p *loggerPromise) Fulfill(parentLogSink logr.LogSink) {
|
|
|
|
sink := parentLogSink
|
2020-10-21 05:49:41 +00:00
|
|
|
if p.name != nil {
|
2021-12-08 13:50:47 +00:00
|
|
|
sink = sink.WithName(*p.name)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.tags != nil {
|
2021-12-08 13:50:47 +00:00
|
|
|
sink = sink.WithValues(p.tags...)
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
p.logger.lock.Lock()
|
2021-12-08 13:50:47 +00:00
|
|
|
p.logger.logger = sink
|
2023-02-01 17:06:36 +00:00
|
|
|
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
|
|
|
|
p.logger.logger = withCallDepth.WithCallDepth(1)
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
p.logger.promise = nil
|
2021-06-25 05:02:01 +00:00
|
|
|
p.logger.lock.Unlock()
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
for _, childPromise := range p.childPromises {
|
2021-12-08 13:50:47 +00:00
|
|
|
childPromise.Fulfill(sink)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// delegatingLogSink is a logsink that delegates to another logr.LogSink.
|
2020-10-21 05:49:41 +00:00
|
|
|
// 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).
|
2023-06-01 17:01:19 +00:00
|
|
|
type delegatingLogSink struct {
|
2021-06-25 05:02:01 +00:00
|
|
|
lock sync.RWMutex
|
2021-12-08 13:50:47 +00:00
|
|
|
logger logr.LogSink
|
2020-10-21 05:49:41 +00:00
|
|
|
promise *loggerPromise
|
2021-12-08 13:50:47 +00:00
|
|
|
info logr.RuntimeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init implements logr.LogSink.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) Init(info logr.RuntimeInfo) {
|
|
|
|
eventuallyFulfillRoot()
|
2021-12-08 13:50:47 +00:00
|
|
|
l.lock.Lock()
|
|
|
|
defer l.lock.Unlock()
|
|
|
|
l.info = info
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Enabled tests whether this Logger is enabled. For example, commandline
|
|
|
|
// flags might be used to set the logging verbosity and disable some info
|
|
|
|
// logs.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) Enabled(level int) bool {
|
|
|
|
eventuallyFulfillRoot()
|
2021-06-25 05:02:01 +00:00
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
2021-12-08 13:50:47 +00:00
|
|
|
return l.logger.Enabled(level)
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info logs a non-error message with the given key/value pairs as context.
|
|
|
|
//
|
|
|
|
// The msg argument should be used to add some constant description to
|
|
|
|
// 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.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
|
|
|
|
eventuallyFulfillRoot()
|
2021-06-25 05:02:01 +00:00
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
2021-12-08 13:50:47 +00:00
|
|
|
l.logger.Info(level, msg, keysAndValues...)
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error logs an error, with the given message and key/value pairs as context.
|
|
|
|
// It functions similarly to calling Info with the "error" named value, but may
|
|
|
|
// have unique behavior, and should be preferred for logging errors (see the
|
|
|
|
// package documentations for more information).
|
|
|
|
//
|
|
|
|
// 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.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
|
|
|
|
eventuallyFulfillRoot()
|
2021-06-25 05:02:01 +00:00
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
|
|
|
l.logger.Error(err, msg, keysAndValues...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithName provides a new Logger with the name appended.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) WithName(name string) logr.LogSink {
|
|
|
|
eventuallyFulfillRoot()
|
2021-06-25 05:02:01 +00:00
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
if l.promise == nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
sink := l.logger.WithName(name)
|
|
|
|
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
|
|
|
|
sink = withCallDepth.WithCallDepth(-1)
|
|
|
|
}
|
|
|
|
return sink
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
res := &delegatingLogSink{logger: l.logger}
|
2020-10-21 05:49:41 +00:00
|
|
|
promise := l.promise.WithName(res, name)
|
|
|
|
res.promise = promise
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// WithValues provides a new Logger with the tags appended.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
|
|
|
|
eventuallyFulfillRoot()
|
2021-06-25 05:02:01 +00:00
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
if l.promise == nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
sink := l.logger.WithValues(tags...)
|
|
|
|
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
|
|
|
|
sink = withCallDepth.WithCallDepth(-1)
|
|
|
|
}
|
|
|
|
return sink
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
res := &delegatingLogSink{logger: l.logger}
|
2020-10-21 05:49:41 +00:00
|
|
|
promise := l.promise.WithValues(res, tags...)
|
|
|
|
res.promise = promise
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-06-01 17:01:19 +00:00
|
|
|
func (l *delegatingLogSink) Fulfill(actual logr.LogSink) {
|
2023-08-28 20:44:55 +00:00
|
|
|
if actual == nil {
|
|
|
|
actual = NullLogSink{}
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
if l.promise != nil {
|
|
|
|
l.promise.Fulfill(actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// newDelegatingLogSink constructs a new DelegatingLogSink which uses
|
2023-02-01 17:06:36 +00:00
|
|
|
// the given logger before its promise is fulfilled.
|
2023-06-01 17:01:19 +00:00
|
|
|
func newDelegatingLogSink(initial logr.LogSink) *delegatingLogSink {
|
|
|
|
l := &delegatingLogSink{
|
2021-06-25 05:02:01 +00:00
|
|
|
logger: initial,
|
2020-10-21 05:49:41 +00:00
|
|
|
promise: &loggerPromise{promisesLock: sync.Mutex{}},
|
|
|
|
}
|
|
|
|
l.promise.logger = l
|
|
|
|
return l
|
|
|
|
}
|