Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetLoggerWithOptions: support flushing #306

Merged
merged 1 commit into from Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 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,18 @@ func ContextualLogger(enabled bool) LoggerOption {
}
}

// FlushLogger provides a callback for flushing data buffered by the logger.
dims marked this conversation as resolved.
Show resolved Hide resolved
//
// Experimental
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func FlushLogger(flush func()) LoggerOption {
return func(o *loggerOptions) {
o.flush = flush
}
}

// LoggerOption implements the functional parameter paradigm for
// SetLoggerWithOptions.
//
Expand All @@ -119,6 +134,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 +153,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 +213,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