Skip to content

Commit

Permalink
Merge pull request #1047 from lwsanty/fix-race-conditions-on-entry
Browse files Browse the repository at this point in the history
fix race conditions on entry
  • Loading branch information
markphelps committed Feb 26, 2020
2 parents 7fab003 + c7278b2 commit 77ab282
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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
Expand Down
8 changes: 4 additions & 4 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,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})

Expand Down Expand Up @@ -238,8 +238,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")
}

0 comments on commit 77ab282

Please sign in to comment.