Skip to content

Commit

Permalink
Merge pull request #1263 from rubensayshi/fix-race
Browse files Browse the repository at this point in the history
  • Loading branch information
sirupsen authored Jan 12, 2022
2 parents 79c5ab6 + 78f8389 commit 85981c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ func (entry *Entry) fireHooks() {
}

func (entry *Entry) write() {
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
serialized, err := entry.Logger.Formatter.Format(entry)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
return
}
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
if _, err := entry.Logger.Out.Write(serialized); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
}
Expand Down
23 changes: 23 additions & 0 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,33 @@ func TestEntryLogfLevel(t *testing.T) {
func TestEntryReportCallerRace(t *testing.T) {
logger := New()
entry := NewEntry(logger)

// logging before SetReportCaller has the highest chance of causing a race condition
// to be detected, but doing it twice just to increase the likelyhood of detecting the race
go func() {
entry.Info("should not race")
}()
go func() {
logger.SetReportCaller(true)
}()
go func() {
entry.Info("should not race")
}()
}

func TestEntryFormatterRace(t *testing.T) {
logger := New()
entry := NewEntry(logger)

// logging before SetReportCaller has the highest chance of causing a race condition
// to be detected, but doing it twice just to increase the likelyhood of detecting the race
go func() {
entry.Info("should not race")
}()
go func() {
logger.SetFormatter(&TextFormatter{})
}()
go func() {
entry.Info("should not race")
}()
}

0 comments on commit 85981c0

Please sign in to comment.