Skip to content
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
19 changes: 8 additions & 11 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ type Logger interface {
// If logger implements the Wither interface, the result of
// logger.With(keyvals...) is returned.
func With(logger Logger, keyvals ...interface{}) Logger {
if w, ok := logger.(Wither); ok {
return w.With(keyvals...)
}
// Limiting the capacity of the stored keyvals ensures that a new
// backing array is created if the slice must grow in Log or With.
// Using the extra capacity without copying risks a data race that
// would violate the Logger interface contract.
n := len(keyvals)
return &withLogger{
logger: logger,
keyvals: keyvals[:n:n],
w, ok := logger.(Wither)
if !ok {
w = &withLogger{logger: logger}
}
return w.With(keyvals...)
}

type withLogger struct {
Expand All @@ -44,6 +37,10 @@ func (l *withLogger) Log(keyvals ...interface{}) error {
}

func (l *withLogger) With(keyvals ...interface{}) Logger {
// Limiting the capacity of the stored keyvals ensures that a new
// backing array is created if the slice must grow in Log or With.
// Using the extra capacity without copying risks a data race that
// would violate the Logger interface contract.
n := len(l.keyvals) + len(keyvals)
return &withLogger{
logger: l.logger,
Expand Down
6 changes: 4 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (

func TestWith(t *testing.T) {
buf := &bytes.Buffer{}
kvs := []interface{}{"a", 123}
logger := log.NewJSONLogger(buf)
logger = log.With(logger, "a", 123)
logger = log.With(logger, kvs...)
kvs[1] = 0 // With should copy its key values
logger = log.With(logger, "b", "c") // With should stack
if err := logger.Log("msg", "message"); err != nil {
t.Fatal(err)
}
if want, have := `{"a":123,"b":"c","msg":"message"}`+"\n", buf.String(); want != have {
t.Errorf("want\n\t%#v, have\n\t%#v", want, have)
t.Errorf("\nwant: %s\nhave: %s", want, have)
}
}

Expand Down