forked from mirror/logrus
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:
parent
d54646ba8e
commit
511792f912
18
entry.go
18
entry.go
|
@ -38,15 +38,23 @@ func (entry *Entry) String() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
||||||
entry.Data[key] = value
|
data := Fields{}
|
||||||
return entry
|
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 {
|
func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
for key, value := range fields {
|
data := Fields{}
|
||||||
entry.WithField(key, value)
|
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 {
|
func (entry *Entry) log(level string, levelInt Level, msg string) string {
|
||||||
|
|
Loading…
Reference in New Issue