Skip to content

Commit

Permalink
SetLoggerWithOptions: support flushing
Browse files Browse the repository at this point in the history
When setting a logger that buffers data in memory, we want klog.Flush to flush
that data. For that the caller has to provide an additional callback because
logr.Logger has no API for flushing.
  • Loading branch information
pohly committed Mar 16, 2022
1 parent a77ab7d commit 454fede
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
22 changes: 18 additions & 4 deletions contextual.go
Expand Up @@ -42,6 +42,10 @@ var (
// none is available.
globalLogger *Logger

// globalLoggerOptions contains the options that were supplied for
// globalLogger.
globalLoggerOptions loggerOptions

// contextualLogger defines whether globalLogger may get called
// directly.
contextualLogger bool
Expand Down Expand Up @@ -87,11 +91,10 @@ func SetLogger(logger logr.Logger) {
// later release.
func SetLoggerWithOptions(logger logr.Logger, opts ...LoggerOption) {
globalLogger = &logger
var o loggerOptions
globalLoggerOptions = loggerOptions{}
for _, opt := range opts {
opt(&o)
opt(&globalLoggerOptions)
}
contextualLogger = o.contextualLogger
}

// ContextualLogger determines whether the logger passed to
Expand All @@ -108,6 +111,13 @@ func ContextualLogger(enabled bool) LoggerOption {
}
}

// FlushLogger provides a callback for flushing data buffered by the logger.
func FlushLogger(flush func()) LoggerOption {
return func(o *loggerOptions) {
o.flush = flush
}
}

// LoggerOption implements the functional parameter paradigm for
// SetLoggerWithOptions.
//
Expand All @@ -119,6 +129,7 @@ type LoggerOption func(o *loggerOptions)

type loggerOptions struct {
contextualLogger bool
flush func()
}

// SetContextualLogger does the same as SetLogger, but in addition the
Expand All @@ -137,6 +148,7 @@ func SetContextualLogger(logger logr.Logger) {
// goroutines invoke log calls, usually during program initialization.
func ClearLogger() {
globalLogger = nil
globalLoggerOptions = loggerOptions{}
}

// EnableContextualLogging controls whether contextual logging is enabled.
Expand Down Expand Up @@ -196,7 +208,9 @@ func TODO() Logger {
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func Background() Logger {
if globalLogger != nil && contextualLogger {
if globalLoggerOptions.contextualLogger {
// Is non-nil because globalLoggerOptions.contextualLogger is
// only true if a logger was set.
return *globalLogger
}

Expand Down
13 changes: 13 additions & 0 deletions contextual_test.go
Expand Up @@ -43,3 +43,16 @@ func ExampleSetLogger() {
// logger after SetLoggerWithOptions with ContextualLogger(false): *klog.klogger
// logger after SetLoggerWithOptions with ContextualLogger(true): logr.discardLogSink
}

func ExampleFlushLogger() {
defer klog.ClearLogger()

// This simple logger doesn't need flushing, but others might.
klog.SetLoggerWithOptions(logr.Discard(), klog.FlushLogger(func() {
fmt.Print("flushing...")
}))
klog.Flush()

// Output:
// flushing...
}
3 changes: 3 additions & 0 deletions klog.go
Expand Up @@ -1097,6 +1097,9 @@ func (l *loggingT) flushAll() {
file.Sync() // ignore error
}
}
if globalLoggerOptions.flush != nil {
globalLoggerOptions.flush()
}
}

// CopyStandardLogTo arranges for messages written to the Go "log" package's
Expand Down

0 comments on commit 454fede

Please sign in to comment.