Skip to content

Commit

Permalink
Merge pull request #320 from pohly/state-restore
Browse files Browse the repository at this point in the history
save and restore state
  • Loading branch information
k8s-ci-robot committed Jun 6, 2022
2 parents a585df9 + 3c90bf9 commit ea35802
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 115 deletions.
38 changes: 13 additions & 25 deletions contextual.go
Expand Up @@ -34,18 +34,6 @@ import (
// mutex locking.

var (
// contextualLoggingEnabled controls whether contextual logging is
// active. Disabling it may have some small performance benefit.
contextualLoggingEnabled = true

// globalLogger is the global Logger chosen by users of klog, nil if
// none is available.
globalLogger *Logger

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

// klogLogger is used as fallback for logging through the normal klog code
// when no Logger is set.
klogLogger logr.Logger = logr.New(&klogger{})
Expand Down Expand Up @@ -81,10 +69,10 @@ func SetLogger(logger logr.Logger) {
// routing log entries through klogr into klog and then into the actual Logger
// backend.
func SetLoggerWithOptions(logger logr.Logger, opts ...LoggerOption) {
globalLogger = &logger
globalLoggerOptions = loggerOptions{}
logging.logger = &logger
logging.loggerOptions = loggerOptions{}
for _, opt := range opts {
opt(&globalLoggerOptions)
opt(&logging.loggerOptions)
}
}

Expand Down Expand Up @@ -119,8 +107,8 @@ type loggerOptions struct {
// Modifying the logger is not thread-safe and should be done while no other
// goroutines invoke log calls, usually during program initialization.
func ClearLogger() {
globalLogger = nil
globalLoggerOptions = loggerOptions{}
logging.logger = nil
logging.loggerOptions = loggerOptions{}
}

// EnableContextualLogging controls whether contextual logging is enabled.
Expand All @@ -132,14 +120,14 @@ func ClearLogger() {
//
// This must be called during initialization before goroutines are started.
func EnableContextualLogging(enabled bool) {
contextualLoggingEnabled = enabled
logging.contextualLoggingEnabled = enabled
}

// FromContext retrieves a logger set by the caller or, if not set,
// falls back to the program's global logger (a Logger instance or klog
// itself).
func FromContext(ctx context.Context) Logger {
if contextualLoggingEnabled {
if logging.contextualLoggingEnabled {
if logger, err := logr.FromContext(ctx); err == nil {
return logger
}
Expand All @@ -160,10 +148,10 @@ func TODO() Logger {
// better receive a logger via its parameters. TODO can be used as a temporary
// solution for such code.
func Background() Logger {
if globalLoggerOptions.contextualLogger {
// Is non-nil because globalLoggerOptions.contextualLogger is
if logging.loggerOptions.contextualLogger {
// Is non-nil because logging.loggerOptions.contextualLogger is
// only true if a logger was set.
return *globalLogger
return *logging.logger
}

return klogLogger
Expand All @@ -172,7 +160,7 @@ func Background() Logger {
// LoggerWithValues returns logger.WithValues(...kv) when
// contextual logging is enabled, otherwise the logger.
func LoggerWithValues(logger Logger, kv ...interface{}) Logger {
if contextualLoggingEnabled {
if logging.contextualLoggingEnabled {
return logger.WithValues(kv...)
}
return logger
Expand All @@ -181,7 +169,7 @@ func LoggerWithValues(logger Logger, kv ...interface{}) Logger {
// LoggerWithName returns logger.WithName(name) when contextual logging is
// enabled, otherwise the logger.
func LoggerWithName(logger Logger, name string) Logger {
if contextualLoggingEnabled {
if logging.contextualLoggingEnabled {
return logger.WithName(name)
}
return logger
Expand All @@ -190,7 +178,7 @@ func LoggerWithName(logger Logger, name string) Logger {
// NewContext returns logr.NewContext(ctx, logger) when
// contextual logging is enabled, otherwise ctx.
func NewContext(ctx context.Context, logger Logger) context.Context {
if contextualLoggingEnabled {
if logging.contextualLoggingEnabled {
return logr.NewContext(ctx, logger)
}
return ctx
Expand Down

0 comments on commit ea35802

Please sign in to comment.