Skip to content

Commit

Permalink
delete unused code (#3504)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosecodym authored Oct 24, 2024
1 parent c37edcd commit 4cc10d3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 383 deletions.
70 changes: 0 additions & 70 deletions pkg/log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package log

import (
"sort"
"sync"

"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -14,15 +12,6 @@ import (
var (
// Global, default log level control.
globalLogLevel levelSetter = zap.NewAtomicLevel()

// Map of name -> level control for independently setting log levels. A new
// control is registered via WithNamedLevel. This map is never cleaned up
// and new entries will overwrite previous values. Currently, this is
// acceptable behavior because WithNamedLevel is used sparingly.
globalControls map[string]levelSetter = make(map[string]levelSetter, 16)
// globalControls is protected (both read and write) by a mutex to make it
// thread safe. Access is low frequency, so performance is not a concern.
globalControlsLock sync.Mutex
)

type levelSetter interface {
Expand All @@ -45,65 +34,6 @@ func SetLevelForControl(control levelSetter, level int8) {
control.SetLevel(zapcore.Level(-level))
}

// SetLevelFor sets the log level for a given named control.
func SetLevelFor(name string, level int8) {
globalControlsLock.Lock()
defer globalControlsLock.Unlock()
if control, ok := globalControls[name]; ok {
SetLevelForControl(control, level)
return
}
// Create a new control so registering a control with the same name will
// inherit the existing level.
globalControls[name] = newAtomicLevelAt(level)
}

// AddLeveler adds a log level control to a logr.Logger.
func AddLeveler(l logr.Logger, control levelSetter) (logr.Logger, error) {
zapLogger, err := getZapLogger(l)
if err != nil {
return l, err
}

zapLogger = zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return NewLevelCore(core, control)
}))
return zapr.NewLogger(zapLogger), nil
}

// WithNamedLevel creates a child logger with a new name and independent log
// level control (see SetLevelFor). NOTE: if name already exists, the existing
// controller will be used, otherwise a new controller is created with level
// matching the parent's log level.
func WithNamedLevel(logger logr.Logger, name string) logr.Logger {
logger = logger.WithName(name)

globalControlsLock.Lock()
defer globalControlsLock.Unlock()

var leveler levelSetter
if currentControl, ok := globalControls[name]; ok {
leveler = currentControl
} else {
leveler = newAtomicLevelAt(findLevel(logger))
globalControls[name] = leveler
}
newLogger, err := AddLeveler(logger, leveler)
if err != nil {
return logger
}
return newLogger
}

// newAtomicLevelAt is a helper function to create a zap.AtomicLevel
// initialized with a level. We cannot use zap.NewAtomicLevelAt here because of
// a quirk with logr levels (see SetLevelForControl).
func newAtomicLevelAt(level int8) zap.AtomicLevel {
control := zap.NewAtomicLevel()
SetLevelForControl(control, level)
return control
}

// findLevel probes a logr.Logger to figure out what level it is at via binary
// search. We only search [0, 128), so worst case is ~7 checks.
func findLevel(logger logr.Logger) int8 {
Expand Down
Loading

0 comments on commit 4cc10d3

Please sign in to comment.