From 511792f912917750f1b62629b2060ca36a59f1be Mon Sep 17 00:00:00 2001 From: Shuhao Wu Date: Mon, 9 Jun 2014 11:31:46 -0400 Subject: [PATCH 1/2] Added support to chain withfields `WithField` and `WithFields` right now doesn't chain properly. Meaning that if you do something like: localLog := logger.WithField("tag", "value") localLog.WithField("v1", "value").Info("message1") localLog.Info("message2") The `v1` will be carried over to `message2`. With this patch, each WithField/WithFields call are isolated. --- entry.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/entry.go b/entry.go index 8cd312e..052697c 100644 --- a/entry.go +++ b/entry.go @@ -38,15 +38,23 @@ func (entry *Entry) String() (string, error) { } func (entry *Entry) WithField(key string, value interface{}) *Entry { - entry.Data[key] = value - return entry + data := Fields{} + for k, v := range entry.Data { + data[k] = v + } + data[key] = value + return &Entry{Logger: entry.Logger, Data: data} } func (entry *Entry) WithFields(fields Fields) *Entry { - for key, value := range fields { - entry.WithField(key, value) + data := Fields{} + for k, v := range entry.Data { + data[k] = v } - return entry + for k, v := range fields { + data[k] = v + } + return &Entry{Logger: entry.Logger, Data: data} } func (entry *Entry) log(level string, levelInt Level, msg string) string { From 7a3a90cb3dace16f6732f12bf05f469e1e921a19 Mon Sep 17 00:00:00 2001 From: Shuhao Wu Date: Mon, 9 Jun 2014 11:53:23 -0400 Subject: [PATCH 2/2] Added test --- entry.go | 7 +------ logrus_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/entry.go b/entry.go index 052697c..08cc15f 100644 --- a/entry.go +++ b/entry.go @@ -38,12 +38,7 @@ func (entry *Entry) String() (string, error) { } func (entry *Entry) WithField(key string, value interface{}) *Entry { - data := Fields{} - for k, v := range entry.Data { - data[k] = v - } - data[key] = value - return &Entry{Logger: entry.Logger, Data: data} + return entry.WithFields(Fields{key: value}) } func (entry *Entry) WithFields(fields Fields) *Entry { diff --git a/logrus_test.go b/logrus_test.go index 96a66a2..82187ae 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -98,3 +98,33 @@ func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) { assert.Equal(t, fields["msg"], "testtest") }) } + +func TestWithFieldsShouldAllowAssignments(t *testing.T) { + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + + localLog := logger.WithFields(Fields{ + "key1": "value1", + }) + + localLog.WithField("key2", "value2").Info("test") + err := json.Unmarshal(buffer.Bytes(), &fields) + assert.Nil(t, err) + + assert.Equal(t, "value2", fields["key2"]) + assert.Equal(t, "value1", fields["key1"]) + + buffer = bytes.Buffer{} + fields = Fields{} + localLog.Info("test") + err = json.Unmarshal(buffer.Bytes(), &fields) + assert.Nil(t, err) + + _, ok := fields["key2"] + assert.Equal(t, false, ok) + assert.Equal(t, "value1", fields["key1"]) +}