Skip to content

Commit

Permalink
contextual logging: hide the additional interface
Browse files Browse the repository at this point in the history
If we only ever expect this to be used via the wrapper function, then we don't
need an interface at all and can simply check for our own type.
  • Loading branch information
pohly committed Mar 10, 2022
1 parent 1f82192 commit f4b4499
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
47 changes: 16 additions & 31 deletions contextual.go
Expand Up @@ -65,54 +65,39 @@ var (
// To remove a backing logr implemention, use ClearLogger. Setting an
// empty logger with SetLogger(logr.Logger{}) does not work.
//
// If the logger instance implements the ContextualLogger interface in addition
// to logr.Logger, then the instance will also be called directly
// by code that retrieves a logger with FromContext, TODO, or Background.
// In that case the logger instance should do its own verbosity checks
// in Enabled because klog will not do that for it.
//
// AddSupportsContext can be used for loggers which don't know about this
// klog specific convention.
// Ideally, loggers should implement the verbosity check in Enabled themselves.
// Then they can get called directly by application code after retrieving the
// logger with FromContext, Background, or TODO. SetLogger needs to know
// whether the logger is ready for that. Wrapping it with WithContextLogging
// provides that information.
//
// Modifying the logger is not thread-safe and should be done while no other
// goroutines invoke log calls, usually during program initialization.
func SetLogger(logger logr.Logger) {
globalLogger = &logger
contextualLogger = false
if contextual, ok := logger.GetSink().(ContextualLogSink); ok && contextual.SupportsContext() {
if contextual, ok := logger.GetSink().(contextualLogSinkWrapper); ok {
contextualLogger = true
// Strip the extra layer, we don't need it anymore.
logger = logr.New(contextual.LogSink)
}
}

// ContextualLogSink is an extension of the logr.LogSink interface that
// indicates to SetLogger whether the logger may get called directly by
// application code. Only the LogSink seen by SetLogger needs to implement
// this, clones of it as they occur during logger.V, logger.WithName or
// logger.WithValues don't need it.
type ContextualLogSink interface {
logr.LogSink

// SupportsContext returns true if the logger may get called directly
// by application code.
SupportsContext() bool
globalLogger = &logger
}

// AddSupportsContext wraps a logger that doesn't know about ContextualLogSink
// so that SetLogger knows that it can also be called directly.
func AddSupportsContext(logger logr.Logger) logr.Logger {
// WithContextLogging wraps a logger so that SetLogger knows that the logger
// can also be called directly.
func WithContextLogging(logger logr.Logger) logr.Logger {
// We can only wrap the LogSink interface but not
// the Logger struct. Besides the LogSink, Logger also
// contains a level. We cannot clone that one,
// so this is slightly broken:
return logr.New(contextualLogSinkWrapper{logger.GetSink()})
}

type contextualLogSinkWrapper struct {
logr.LogSink
}

func (c contextualLogSinkWrapper) SupportsContext() bool {
return true
}

var _ ContextualLogSink = contextualLogSinkWrapper{}

// ClearLogger removes a backing Logger implementation if one was set earlier
// with SetLogger.
//
Expand Down
16 changes: 6 additions & 10 deletions contextual_test.go
Expand Up @@ -24,17 +24,13 @@ import (
)

func ExampleSetLogger() {
logger := logr.Discard()
klog.SetLogger(logger)
equal := logger == klog.Background()
fmt.Printf("original logr.Discard logger can be used directly: %v\n", equal)
klog.SetLogger(logr.Discard())
fmt.Printf("after setting logr.Discard as logger: %T\n", klog.Background().GetSink())

logger = klog.AddSupportsContext(logger)
klog.SetLogger(logger)
equal = logger == klog.Background()
fmt.Printf("wrapped logger can be used directly: %v\n", equal)
klog.SetLogger(klog.WithContextLogging(logr.Discard()))
fmt.Printf("after wrapping it: %T\n", klog.Background().GetSink())

// Output:
// original logr.Discard logger can be used directly: false
// wrapped logger can be used directly: true
// after setting logr.Discard as logger: *klog.klogger
// after wrapping it: logr.discardLogSink
}

0 comments on commit f4b4499

Please sign in to comment.