diff --git a/klog.go b/klog.go index e850db6b0..e042e2bc6 100644 --- a/klog.go +++ b/klog.go @@ -780,7 +780,7 @@ func (l *loggingT) errorS(err error, loggr logr.Logger, filter LogFilter, depth loggr.Error(err, msg, keysAndValues...) return } - l.printS(err, depth+1, msg, keysAndValues...) + l.printS(err, errorLog, depth+1, msg, keysAndValues...) } // if loggr is specified, will call loggr.Info, otherwise output with logging module. @@ -792,12 +792,12 @@ func (l *loggingT) infoS(loggr logr.Logger, filter LogFilter, depth int, msg str loggr.Info(msg, keysAndValues...) return } - l.printS(nil, depth+1, msg, keysAndValues...) + l.printS(nil, infoLog, depth+1, 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, depth int, msg string, keysAndValues ...interface{}) { +// set log severity by s +func (l *loggingT) printS(err error, s severity, depth int, msg string, keysAndValues ...interface{}) { b := &bytes.Buffer{} b.WriteString(fmt.Sprintf("%q", msg)) if err != nil { @@ -805,12 +805,6 @@ func (l *loggingT) printS(err error, depth int, msg string, keysAndValues ...int b.WriteString(fmt.Sprintf("err=%q", err.Error())) } kvListFormat(b, keysAndValues...) - var s severity - if err == nil { - s = infoLog - } else { - s = errorLog - } l.printDepth(s, logging.logr, nil, depth+1, b) } diff --git a/klog_test.go b/klog_test.go index ecdc19a39..ba4349b1c 100644 --- a/klog_test.go +++ b/klog_test.go @@ -952,17 +952,31 @@ func TestErrorS(t *testing.T) { myErrorS, } for _, f := range functions { - logging.file[errorLog] = &flushBuffer{} - f(fmt.Errorf("update status failed"), "Failed to update pod status", "pod", "kubedns") - var line int - format := "E0102 15:04:05.067890 1234 klog_test.go:%d] \"Failed to update pod status\" err=\"update status failed\" pod=\"kubedns\"\n" - n, err := fmt.Sscanf(contents(errorLog), format, &line) - if n != 1 || err != nil { - t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(errorLog)) + var errorList = []struct { + err error + format string + }{ + { + err: fmt.Errorf("update status failed"), + format: "E0102 15:04:05.067890 1234 klog_test.go:%d] \"Failed to update pod status\" err=\"update status failed\" pod=\"kubedns\"\n", + }, + { + err: nil, + format: "E0102 15:04:05.067890 1234 klog_test.go:%d] \"Failed to update pod status\" pod=\"kubedns\"\n", + }, } - want := fmt.Sprintf(format, line) - if contents(errorLog) != want { - t.Errorf("ErrorS has wrong format: \n got:\t%s\nwant:\t%s", contents(errorLog), want) + for _, e := range errorList { + logging.file[errorLog] = &flushBuffer{} + f(e.err, "Failed to update pod status", "pod", "kubedns") + var line int + n, err := fmt.Sscanf(contents(errorLog), e.format, &line) + if n != 1 || err != nil { + t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(errorLog)) + } + want := fmt.Sprintf(e.format, line) + if contents(errorLog) != want { + t.Errorf("ErrorS has wrong format: \n got:\t%s\nwant:\t%s", contents(errorLog), want) + } } } }