From c7278b2d7a38c9dae5bf70203d64696be84f6755 Mon Sep 17 00:00:00 2001 From: lwsanty Date: Wed, 23 Oct 2019 20:43:07 +0300 Subject: [PATCH] fix race conditions on entry closes #1046 --- entry.go | 2 ++ entry_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/entry.go b/entry.go index 7e2c899..b3a8082 100644 --- a/entry.go +++ b/entry.go @@ -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 diff --git a/entry_test.go b/entry_test.go index f764085..e1cb1b5 100644 --- a/entry_test.go +++ b/entry_test.go @@ -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", ) -} \ No newline at end of file + assert.Contains(t, buffer.String(), "warn") +}