Skip to content

Commit

Permalink
Clean up implementation of logging message helper functions
Browse files Browse the repository at this point in the history
The method signature of logging.Msg() was artificially limiting.
The implementation of logging.Msgf() converted multiple params to an
array instead of passing them through to fmt.Sprintf().
  • Loading branch information
stu-gott committed Jan 9, 2017
1 parent f6c02df commit 84eeb47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func (e LogError) Error() string {
return e.message
}

func (l FilteredLogger) Msg(msg string) {
func (l FilteredLogger) Msg(msg interface{}) {
l.Log("msg", msg)
}

func (l FilteredLogger) Msgf(msg string, args ...interface{}) {
l.Msg(fmt.Sprintf(msg, args))
l.Msg(fmt.Sprintf(msg, args...))
}

func (l FilteredLogger) Log(params ...interface{}) error {
Expand Down
24 changes: 24 additions & 0 deletions pkg/logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logging

import (
"errors"
"kubevirt.io/kubevirt/pkg/api"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -283,3 +284,26 @@ func TestObject(t *testing.T) {
assert(t, logEntry[6].(string) == "uid", "Logged line did not contain UUID")
tearDown()
}

func TestError(t *testing.T) {
setUp()
log := MakeLogger(MockLogger{})
log.SetLogLevel(DEBUG)
err := errors.New("Test error")
log.Error().Log(err)
assert(t, logCalled, "Error was not logged via .Log()")

logCalled = false
log.Msg(err)
assert(t, logCalled, "Error was not logged via .Msg()")

logCalled = false
// using more than one parameter in format string
log.Msgf("[%d] %s", 1, err)
assert(t, logCalled, "Error was not logged via .Msgf()")

logCalled = false
log.Msgf("%s", err)
assert(t, logCalled, "Error was not logged via .Msgf()")
tearDown()
}

0 comments on commit 84eeb47

Please sign in to comment.