From d521fd2e1f4fba741c08db6a22ca37e76f69ad6f Mon Sep 17 00:00:00 2001 From: Gregor Noczinski Date: Sat, 19 Sep 2020 19:58:31 +0200 Subject: [PATCH 1/2] Forwards keysAndValues as an slice and not as a slice of slices --- klog.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/klog.go b/klog.go index d8c4641a..767a18c9 100644 --- a/klog.go +++ b/klog.go @@ -759,7 +759,7 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, file string, // 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 { - loggr.Error(err, msg, keysAndValues) + loggr.Error(err, msg, keysAndValues...) return } l.printS(err, msg, keysAndValues...) @@ -768,7 +768,7 @@ func (l *loggingT) errorS(err error, loggr logr.Logger, msg string, keysAndValue // if loggr is specified, will call loggr.Info, otherwise output with logging module. func (l *loggingT) infoS(loggr logr.Logger, msg string, keysAndValues ...interface{}) { if loggr != nil { - loggr.Info(msg, keysAndValues) + loggr.Info(msg, keysAndValues...) return } l.printS(nil, msg, keysAndValues...) From 17fe943149d517ebf760d8d2a803ba6d2fd4f02b Mon Sep 17 00:00:00 2001 From: Gregor Noczinski Date: Sun, 20 Sep 2020 11:54:13 +0200 Subject: [PATCH 2/2] Added unit tests --- klog_test.go | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/klog_test.go b/klog_test.go index 8003c3d6..8c8fa67d 100644 --- a/klog_test.go +++ b/klog_test.go @@ -21,10 +21,12 @@ import ( "errors" "flag" "fmt" + "github.com/go-logr/logr" "io/ioutil" stdLog "log" "os" "path/filepath" + "reflect" "regexp" "runtime" "strconv" @@ -1074,3 +1076,156 @@ func TestSetVState(t *testing.T) { t.Errorf("setVState method doesn't configure loggingT values' verbosity, vmodule or filterLength:\nwant:\n\tverbosity:\t%v\n\tvmodule:\t%v\n\tfilterLength:\t%v\ngot:\n\tverbosity:\t%v\n\tvmodule:\t%v\n\tfilterLength:\t%v", want.verbosity, want.vmodule, want.filterLength, target.verbosity, target.vmodule, target.filterLength) } } + +func TestInfoSWithLogr(t *testing.T) { + logger := new(testLogr) + + testDataInfo := []struct { + msg string + keysValues []interface{} + expected testLogrEntry + }{{ + msg: "foo", + keysValues: []interface{}{}, + expected: testLogrEntry{ + severity: infoLog, + msg: "foo", + keysAndValues: []interface{}{}, + }, + }, { + msg: "bar", + keysValues: []interface{}{"a", 1}, + expected: testLogrEntry{ + severity: infoLog, + msg: "bar", + keysAndValues: []interface{}{"a", 1}, + }, + }} + + for _, data := range testDataInfo { + t.Run(data.msg, func(t *testing.T) { + SetLogger(logger) + defer SetLogger(nil) + defer logger.reset() + + InfoS(data.msg, data.keysValues...) + + if !reflect.DeepEqual(logger.entries, []testLogrEntry{data.expected}) { + t.Errorf("expected: %+v; but got: %+v", []testLogrEntry{data.expected}, logger.entries) + } + }) + } +} + +func TestErrorSWithLogr(t *testing.T) { + logger := new(testLogr) + + testError := errors.New("testError") + + testDataInfo := []struct { + err error + msg string + keysValues []interface{} + expected testLogrEntry + }{{ + err: testError, + msg: "foo1", + keysValues: []interface{}{}, + expected: testLogrEntry{ + severity: errorLog, + msg: "foo1", + keysAndValues: []interface{}{}, + err: testError, + }, + }, { + err: testError, + msg: "bar1", + keysValues: []interface{}{"a", 1}, + expected: testLogrEntry{ + severity: errorLog, + msg: "bar1", + keysAndValues: []interface{}{"a", 1}, + err: testError, + }, + }, { + err: nil, + msg: "foo2", + keysValues: []interface{}{}, + expected: testLogrEntry{ + severity: errorLog, + msg: "foo2", + keysAndValues: []interface{}{}, + err: nil, + }, + }, { + err: nil, + msg: "bar2", + keysValues: []interface{}{"a", 1}, + expected: testLogrEntry{ + severity: errorLog, + msg: "bar2", + keysAndValues: []interface{}{"a", 1}, + err: nil, + }, + }} + + for _, data := range testDataInfo { + t.Run(data.msg, func(t *testing.T) { + SetLogger(logger) + defer SetLogger(nil) + defer logger.reset() + + ErrorS(data.err, data.msg, data.keysValues...) + + if !reflect.DeepEqual(logger.entries, []testLogrEntry{data.expected}) { + t.Errorf("expected: %+v; but got: %+v", []testLogrEntry{data.expected}, logger.entries) + } + }) + } +} + +type testLogr struct { + entries []testLogrEntry + mutex sync.Mutex +} + +type testLogrEntry struct { + severity severity + msg string + keysAndValues []interface{} + err error +} + +func (l *testLogr) reset() { + l.mutex.Lock() + defer l.mutex.Unlock() + l.entries = []testLogrEntry{} +} + +func (l *testLogr) Info(msg string, keysAndValues ...interface{}) { + l.mutex.Lock() + defer l.mutex.Unlock() + l.entries = append(l.entries, testLogrEntry{ + severity: infoLog, + msg: msg, + keysAndValues: keysAndValues, + }) +} + +func (l *testLogr) Error(err error, msg string, keysAndValues ...interface{}) { + l.mutex.Lock() + defer l.mutex.Unlock() + l.entries = append(l.entries, testLogrEntry{ + severity: errorLog, + msg: msg, + keysAndValues: keysAndValues, + err: err, + }) +} + +func (l *testLogr) Enabled() bool { panic("not implemented") } +func (l *testLogr) V(int) logr.Logger { panic("not implemented") } +func (l *testLogr) WithName(string) logr.Logger { panic("not implemented") } +func (l *testLogr) WithValues(...interface{}) logr.Logger { + panic("not implemented") +}