diff --git a/klog.go b/klog.go index 5f8e67ce..de49e85e 100644 --- a/klog.go +++ b/klog.go @@ -751,17 +751,27 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.InfoLogger, file stri l.output(s, logr, buf, file, line, alsoToStderr) } -// printS if loggr is specified, no need to output with logging module. If -// err arguments is specified, will call logr.Error, or output to errorLog severity -func (l *loggingT) printS(err error, loggr logr.Logger, msg string, keysAndValues ...interface{}) { +// if loggr is specified, will call loggr.Error, otherwise output with logging module. +func (l *loggingT) errorS(err error, loggr logr.Logger, msg string, keysAndValues ...interface{}) { if loggr != nil { - if err != nil { - loggr.Error(err, msg, keysAndValues) - } else { - loggr.Info(msg, keysAndValues) - } + loggr.Error(err, msg, keysAndValues) return } + l.printS(err, msg, keysAndValues...) +} + +// if loggr is specified, will call loggr.Info, otherwise output with logging module. +func (l *loggingT) infoS(loggr logr.InfoLogger, msg string, keysAndValues ...interface{}) { + if loggr != nil { + loggr.Info(msg, keysAndValues) + return + } + l.printS(nil, msg, keysAndValues...) +} + +// printS is called from infoS and errorS if loggr is not specified. +// if err arguments is specified, will output to errorLog severity +func (l *loggingT) printS(err error, msg string, keysAndValues ...interface{}) { b := &bytes.Buffer{} b.WriteString(fmt.Sprintf("%q", msg)) if err != nil { @@ -775,7 +785,7 @@ func (l *loggingT) printS(err error, loggr logr.Logger, msg string, keysAndValue } else { s = errorLog } - l.printDepth(s, logging.logr, 1, b) + l.printDepth(s, logging.logr, 2, b) } const missingValue = "(MISSING)" @@ -1304,11 +1314,7 @@ func (v Verbose) Infof(format string, args ...interface{}) { // See the documentation of V for usage. func (v Verbose) InfoS(msg string, keysAndValues ...interface{}) { if v.enabled { - if v.logr != nil { - v.logr.Info(msg, keysAndValues) - return - } - logging.printS(nil, nil, msg, keysAndValues...) + logging.infoS(v.logr, msg, keysAndValues) } } @@ -1345,7 +1351,7 @@ func Infof(format string, args ...interface{}) { // output: // >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kubedns" status="ready" func InfoS(msg string, keysAndValues ...interface{}) { - logging.printS(nil, logging.logr, msg, keysAndValues...) + logging.infoS(logging.logr, msg, keysAndValues...) } // Warning logs to the WARNING and INFO logs. @@ -1406,7 +1412,7 @@ func Errorf(format string, args ...interface{}) { // output: // >> E1025 00:15:15.525108 1 controller_utils.go:114] "Failed to update pod status" err="timeout" func ErrorS(err error, msg string, keysAndValues ...interface{}) { - logging.printS(err, logging.logr, msg, keysAndValues...) + logging.errorS(err, logging.logr, msg, keysAndValues...) } // Fatal logs to the FATAL, ERROR, WARNING, and INFO logs,