From 511792f912917750f1b62629b2060ca36a59f1be Mon Sep 17 00:00:00 2001 From: Shuhao Wu Date: Mon, 9 Jun 2014 11:31:46 -0400 Subject: [PATCH] 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 {