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.
This commit is contained in:
Shuhao Wu 2014-06-09 11:31:46 -04:00
parent d54646ba8e
commit 511792f912
1 changed files with 13 additions and 5 deletions

View File

@ -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 {