Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

pkg/log: correctly handle var-arg printf params #3516

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
pkg/log: correctly handle var-arg printf params
  • Loading branch information
euank committed Jan 6, 2017
commit 3e109693d63b46e25a2a800402cf7b0691f6feeb
9 changes: 2 additions & 7 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (l *Logger) Error(e error) {

// Errorf is a convenience function for formatting and printing errors.
func (l *Logger) Errorf(format string, a ...interface{}) {
l.Print(l.formatErr(fmt.Errorf(format, a), ""))
l.Print(l.formatErr(fmt.Errorf(format, a...), ""))
}

// FatalE prints a string and error then calls os.Exit(254).
Expand All @@ -124,16 +124,11 @@ func (l *Logger) FatalE(msg string, e error) {

// Fatalf prints an error then calls os.Exit(254).
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.Print(l.formatErr(fmt.Errorf(format, a), ""))
l.Print(l.formatErr(fmt.Errorf(format, a...), ""))
os.Exit(254)
}

// PanicE prints a string and error then calls panic.
func (l *Logger) PanicE(msg string, e error) {
l.Panic(l.formatErr(e, msg))
}

// Panicf prints an error then calls panic.
func (l *Logger) Panicf(format string, a ...interface{}) {
l.Panic(l.formatErr(fmt.Errorf(format, a), ""))
}
13 changes: 13 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ func TestLogOutput(t *testing.T) {
}
}
}

func TestLogFormatting(t *testing.T) {
var logBuf bytes.Buffer
l := New(&logBuf, "prefix", false)

l.Errorf("format args: %s %d", "string", 1)

expected := "prefix: format args: string 1\n"

if logBuf.String() != expected {
t.Errorf("expected %q, got %q", expected, logBuf.String())
}
}