fix race conditions on entry

closes #1046
This commit is contained in:
lwsanty 2019-10-23 20:43:07 +03:00
parent d5d4df1108
commit c7278b2d7a
2 changed files with 6 additions and 4 deletions

View File

@ -113,6 +113,8 @@ func (entry *Entry) WithField(key string, value interface{}) *Entry {
// Add a map of fields to the Entry.
func (entry *Entry) WithFields(fields Fields) *Entry {
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
data := make(Fields, len(entry.Data)+len(fields))
for k, v := range entry.Data {
data[k] = v

View File

@ -134,7 +134,7 @@ func TestEntryWithIncorrectField(t *testing.T) {
fn := func() {}
e := Entry{}
e := &Entry{Logger: New()}
eWithFunc := e.WithFields(Fields{"func": fn})
eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
@ -162,8 +162,8 @@ func TestEntryLogfLevel(t *testing.T) {
entry := NewEntry(logger)
entry.Logf(DebugLevel, "%s", "debug")
assert.NotContains(t, buffer.String(), "debug", )
assert.NotContains(t, buffer.String(), "debug")
entry.Logf(WarnLevel, "%s", "warn")
assert.Contains(t, buffer.String(), "warn", )
assert.Contains(t, buffer.String(), "warn")
}