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

save and restore state #320

Merged
merged 2 commits into from Jun 6, 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
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 {
thockin marked this conversation as resolved.
Show resolved Hide resolved
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