Skip to content

Commit

Permalink
Merge pull request #217 from yuzhiquan/fix-erros-loglevel
Browse files Browse the repository at this point in the history
Fix err is nil,ErrorS should not change the log level
  • Loading branch information
k8s-ci-robot committed Mar 7, 2021
2 parents 14dec33 + 30ac266 commit c1fc6e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
14 changes: 4 additions & 10 deletions klog.go
Expand Up @@ -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.
Expand All @@ -792,25 +792,19 @@ 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 {
b.WriteByte(' ')
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)
}

Expand Down
34 changes: 24 additions & 10 deletions klog_test.go
Expand Up @@ -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)
}
}
}
}
Expand Down

0 comments on commit c1fc6e6

Please sign in to comment.