summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/klog/v2/klog.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/klog/v2/klog.go')
-rw-r--r--vendor/k8s.io/klog/v2/klog.go106
1 files changed, 75 insertions, 31 deletions
diff --git a/vendor/k8s.io/klog/v2/klog.go b/vendor/k8s.io/klog/v2/klog.go
index 1e187f763..45efbb075 100644
--- a/vendor/k8s.io/klog/v2/klog.go
+++ b/vendor/k8s.io/klog/v2/klog.go
@@ -509,7 +509,7 @@ type loggingT struct {
addDirHeader bool
// If set, all output will be redirected unconditionally to the provided logr.Logger
- logr logr.Logger
+ logr *logr.Logger
// If true, messages will not be propagated to lower severity log levels
oneOutput bool
@@ -698,11 +698,11 @@ func (buf *buffer) someDigits(i, d int) int {
return copy(buf.tmp[i:], buf.tmp[j:])
}
-func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
+func (l *loggingT) println(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
buf, file, line := l.header(s, 0)
- // if logr is set, we clear the generated header as we rely on the backing
- // logr implementation to print headers
- if logr != nil {
+ // if logger is set, we clear the generated header as we rely on the backing
+ // logger implementation to print headers
+ if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
@@ -710,18 +710,18 @@ func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args
args = filter.Filter(args)
}
fmt.Fprintln(buf, args...)
- l.output(s, logr, buf, 0 /* depth */, file, line, false)
+ l.output(s, logger, buf, 0 /* depth */, file, line, false)
}
-func (l *loggingT) print(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
- l.printDepth(s, logr, filter, 1, args...)
+func (l *loggingT) print(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
+ l.printDepth(s, logger, filter, 1, args...)
}
-func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, depth int, args ...interface{}) {
+func (l *loggingT) printDepth(s severity, logger *logr.Logger, filter LogFilter, depth int, args ...interface{}) {
buf, file, line := l.header(s, depth)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
- if logr != nil {
+ if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
@@ -732,14 +732,14 @@ func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, de
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
- l.output(s, logr, buf, depth, file, line, false)
+ l.output(s, logger, buf, depth, file, line, false)
}
-func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format string, args ...interface{}) {
+func (l *loggingT) printf(s severity, logger *logr.Logger, filter LogFilter, format string, args ...interface{}) {
buf, file, line := l.header(s, 0)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
- if logr != nil {
+ if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
@@ -750,17 +750,17 @@ func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
- l.output(s, logr, buf, 0 /* depth */, file, line, false)
+ l.output(s, logger, buf, 0 /* depth */, file, line, false)
}
// printWithFileLine behaves like print but uses the provided file and line number. If
// alsoLogToStderr is true, the log message always appears on standard error; it
// will also appear in the log file unless --logtostderr is set.
-func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
+func (l *loggingT) printWithFileLine(s severity, logger *logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
buf := l.formatHeader(s, file, line)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
- if logr != nil {
+ if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
@@ -771,28 +771,28 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFil
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
- l.output(s, logr, buf, 2 /* depth */, file, line, alsoToStderr)
+ l.output(s, logger, buf, 2 /* depth */, file, line, alsoToStderr)
}
// if loggr is specified, will call loggr.Error, otherwise output with logging module.
-func (l *loggingT) errorS(err error, loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
+func (l *loggingT) errorS(err error, logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
if filter != nil {
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
}
- if loggr != nil {
- logr.WithCallDepth(loggr, depth+2).Error(err, msg, keysAndValues...)
+ if logger != nil {
+ logger.WithCallDepth(depth+2).Error(err, msg, keysAndValues...)
return
}
l.printS(err, errorLog, depth+1, msg, keysAndValues...)
}
// if loggr is specified, will call loggr.Info, otherwise output with logging module.
-func (l *loggingT) infoS(loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
+func (l *loggingT) infoS(logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
if filter != nil {
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
}
- if loggr != nil {
- logr.WithCallDepth(loggr, depth+2).Info(msg, keysAndValues...)
+ if logger != nil {
+ logger.WithCallDepth(depth+2).Info(msg, keysAndValues...)
return
}
l.printS(nil, infoLog, depth+1, msg, keysAndValues...)
@@ -862,11 +862,23 @@ func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) {
// Use as:
// ...
// klog.SetLogger(zapr.NewLogger(zapLog))
+//
+// To remove a backing logr implemention, use ClearLogger. Setting an
+// empty logger with SetLogger(logr.Logger{}) does not work.
func SetLogger(logr logr.Logger) {
logging.mu.Lock()
defer logging.mu.Unlock()
- logging.logr = logr
+ logging.logr = &logr
+}
+
+// ClearLogger removes a backing logr implementation if one was set earlier
+// with SetLogger.
+func ClearLogger() {
+ logging.mu.Lock()
+ defer logging.mu.Unlock()
+
+ logging.logr = nil
}
// SetOutput sets the output destination for all severities
@@ -904,7 +916,7 @@ func LogToStderr(stderr bool) {
}
// output writes the data to the log files and releases the buffer.
-func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
+func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
l.mu.Lock()
if l.traceLocation.isSet() {
if l.traceLocation.match(file, line) {
@@ -916,9 +928,9 @@ func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, f
// TODO: set 'severity' and caller information as structured log info
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
if s == errorLog {
- logr.WithCallDepth(l.logr, depth+3).Error(nil, string(data))
+ l.logr.WithCallDepth(depth+3).Error(nil, string(data))
} else {
- logr.WithCallDepth(log, depth+3).Info(string(data))
+ log.WithCallDepth(depth + 3).Info(string(data))
}
} else if l.toStderr {
os.Stderr.Write(data)
@@ -1269,7 +1281,7 @@ func (l *loggingT) setV(pc uintptr) Level {
// See the documentation of V for more information.
type Verbose struct {
enabled bool
- logr logr.Logger
+ logr *logr.Logger
filter LogFilter
}
@@ -1277,7 +1289,8 @@ func newVerbose(level Level, b bool) Verbose {
if logging.logr == nil {
return Verbose{b, nil, logging.filter}
}
- return Verbose{b, logging.logr.V(int(level)), logging.filter}
+ v := logging.logr.V(int(level))
+ return Verbose{b, &v, logging.filter}
}
// V reports whether verbosity at the call site is at least the requested level.
@@ -1315,9 +1328,14 @@ func V(level Level) Verbose {
if runtime.Callers(2, logging.pcs[:]) == 0 {
return newVerbose(level, false)
}
- v, ok := logging.vmap[logging.pcs[0]]
+ // runtime.Callers returns "return PCs", but we want
+ // to look up the symbolic information for the call,
+ // so subtract 1 from the PC. runtime.CallersFrames
+ // would be cleaner, but allocates.
+ pc := logging.pcs[0] - 1
+ v, ok := logging.vmap[pc]
if !ok {
- v = logging.setV(logging.pcs[0])
+ v = logging.setV(pc)
}
return newVerbose(level, v >= level)
}
@@ -1573,6 +1591,15 @@ func (ref ObjectRef) String() string {
return ref.Name
}
+// MarshalLog ensures that loggers with support for structured output will log
+// as a struct by removing the String method via a custom type.
+func (ref ObjectRef) MarshalLog() interface{} {
+ type or ObjectRef
+ return or(ref)
+}
+
+var _ logr.Marshaler = ObjectRef{}
+
// KMetadata is a subset of the kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface
// this interface may expand in the future, but will always be a subset of the
// kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface
@@ -1603,3 +1630,20 @@ func KRef(namespace, name string) ObjectRef {
Namespace: namespace,
}
}
+
+// KObjs returns slice of ObjectRef from an slice of ObjectMeta
+func KObjs(arg interface{}) []ObjectRef {
+ s := reflect.ValueOf(arg)
+ if s.Kind() != reflect.Slice {
+ return nil
+ }
+ objectRefs := make([]ObjectRef, 0, s.Len())
+ for i := 0; i < s.Len(); i++ {
+ if v, ok := s.Index(i).Interface().(KMetadata); ok {
+ objectRefs = append(objectRefs, KObj(v))
+ } else {
+ return nil
+ }
+ }
+ return objectRefs
+}