From 396f8eefaa6f899b05717791043b2b02ba768bd5 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Thu, 6 Aug 2015 21:00:57 -0300 Subject: [PATCH 1/3] Make log method receive a copy of Entry structure to avoid race conditions Fixes #216 Signed-off-by: Marcos Lilljedahl --- entry.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entry.go b/entry.go index 699ea03..8a522f0 100644 --- a/entry.go +++ b/entry.go @@ -70,12 +70,12 @@ func (entry *Entry) WithFields(fields Fields) *Entry { return &Entry{Logger: entry.Logger, Data: data} } -func (entry *Entry) log(level Level, msg string) { +func (entry Entry) log(level Level, msg string) { entry.Time = time.Now() entry.Level = level entry.Message = msg - if err := entry.Logger.Hooks.Fire(level, entry); err != nil { + if err := entry.Logger.Hooks.Fire(level, &entry); err != nil { entry.Logger.mu.Lock() fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) entry.Logger.mu.Unlock() From 35f12fb760875958f2a8d2d3520b8e91fdc4e143 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Thu, 6 Aug 2015 21:19:22 -0300 Subject: [PATCH 2/3] Fix panic return type Signed-off-by: Marcos Lilljedahl --- entry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entry.go b/entry.go index 8a522f0..001c4c7 100644 --- a/entry.go +++ b/entry.go @@ -100,7 +100,7 @@ func (entry Entry) log(level Level, msg string) { // panic() to use in Entry#Panic(), we avoid the allocation by checking // directly here. if level <= PanicLevel { - panic(entry) + panic(&entry) } } From 8280b8b9a6e01f1b48ae2079f8d5114cd3b93592 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 7 Aug 2015 18:29:20 -0300 Subject: [PATCH 3/3] Add comment to log function Signed-off-by: Marcos Lilljedahl --- entry.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/entry.go b/entry.go index 001c4c7..04673a0 100644 --- a/entry.go +++ b/entry.go @@ -70,6 +70,8 @@ func (entry *Entry) WithFields(fields Fields) *Entry { return &Entry{Logger: entry.Logger, Data: data} } +// This function is not declared with a pointer value because otherwise +// race conditions will occur when using multiple goroutines func (entry Entry) log(level Level, msg string) { entry.Time = time.Now() entry.Level = level