Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: avoid '%!F(MISSING)' in logs #6555

Merged
merged 1 commit into from
Jan 29, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,37 @@ func (l *StandardLogger) GetLevel() Level {

// Debug logs at debug level
func (l *StandardLogger) Debug(fmt string, a ...interface{}) {
if len(a) == 0 {
l.logger.WithFields(l.getFields()).Debug(fmt)
return
}
l.logger.WithFields(l.getFields()).Debugf(fmt, a...)
}

// Info logs at info level
func (l *StandardLogger) Info(fmt string, a ...interface{}) {
if len(a) == 0 {
l.logger.WithFields(l.getFields()).Info(fmt)
return
}
l.logger.WithFields(l.getFields()).Infof(fmt, a...)
}

// Error logs at error level
func (l *StandardLogger) Error(fmt string, a ...interface{}) {
if len(a) == 0 {
l.logger.WithFields(l.getFields()).Error(fmt)
return
}
l.logger.WithFields(l.getFields()).Errorf(fmt, a...)
}

// Warn logs at warn level
func (l *StandardLogger) Warn(fmt string, a ...interface{}) {
if len(a) == 0 {
l.logger.WithFields(l.getFields()).Warn(fmt)
return
}
l.logger.WithFields(l.getFields()).Warnf(fmt, a...)
}

Expand Down
33 changes: 33 additions & 0 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/rand"
"net/url"
"strings"
"testing"

Expand Down Expand Up @@ -45,6 +46,38 @@ func TestCaptureWarningWithErrorSet(t *testing.T) {
}
}

func TestNoFormattingForSingleString(t *testing.T) {
buf := bytes.Buffer{}
logger := New()
logger.SetOutput(&buf)
logger.SetLevel(Debug)

// NOTE(sr): This construction is somewhat realistic: If we fed logger.Error()
// a format string but no args, the golang linters would yell. The indirection
// taken here is enough to not trigger linters.
x := url.PathEscape("/foo/bar/bar")
logger.Debug(x)
logger.Info(x)
logger.Warn(x)
logger.Error(x)

exp := `"%2Ffoo%2Fbar%2Fbar"`
expected := []string{
`level=error msg=` + exp,
`level=warning msg=` + exp,
`level=info msg=` + exp,
`level=debug msg=` + exp,
}
for _, exp := range expected {
if !strings.Contains(buf.String(), exp) {
t.Errorf("expected string %q not found in logs", exp)
}
}
if t.Failed() {
t.Logf("actual output:\n%s", buf.String())
}
}

func TestWithFieldsOverrides(t *testing.T) {
logger := New().
WithFields(map[string]interface{}{"context": "contextvalue"}).
Expand Down