Browse Source

fix ReportCaller race condition

tags/v1.4.0
georlav 7 months ago
parent
commit
b9d451406d
2 changed files with 19 additions and 0 deletions
  1. 2
    0
      entry.go
  2. 17
    0
      logrus_test.go

+ 2
- 0
entry.go View File

@@ -206,7 +206,9 @@ func (entry Entry) log(level Level, msg string) {
206 206
 	entry.Level = level
207 207
 	entry.Message = msg
208 208
 	if entry.Logger.ReportCaller {
209
+		entry.Logger.mu.Lock()
209 210
 		entry.Caller = getCaller()
211
+		entry.Logger.mu.Unlock()
210 212
 	}
211 213
 
212 214
 	entry.fireHooks()

+ 17
- 0
logrus_test.go View File

@@ -743,3 +743,20 @@ func TestReportCallerOnTextFormatter(t *testing.T) {
743 743
 	l.Formatter.(*TextFormatter).DisableColors = true
744 744
 	l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")
745 745
 }
746
+
747
+func TestSetReportCallerRace(t *testing.T) {
748
+	l := New()
749
+	l.Out = ioutil.Discard
750
+	l.SetReportCaller(true)
751
+
752
+	var wg sync.WaitGroup
753
+	wg.Add(100)
754
+
755
+	for i := 0; i < 100; i++ {
756
+		go func() {
757
+			l.Error("Some Error")
758
+			wg.Done()
759
+		}()
760
+	}
761
+	wg.Wait()
762
+}

Loading…
Cancel
Save