Skip to content

Commit

Permalink
ErrorS(nil, ...) should call loggr.Error(nil, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
tahsinrahman committed May 29, 2020
1 parent 9e86e5a commit 49123d4
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 49123d4

Please sign in to comment.