fix ReportCaller race condition

This commit is contained in:
georlav 2019-03-04 20:38:10 +02:00
parent c9b4f5af6d
commit b9d451406d
2 changed files with 19 additions and 0 deletions

View File

@ -206,7 +206,9 @@ func (entry Entry) log(level Level, msg string) {
entry.Level = level
entry.Message = msg
if entry.Logger.ReportCaller {
entry.Logger.mu.Lock()
entry.Caller = getCaller()
entry.Logger.mu.Unlock()
}
entry.fireHooks()

View File

@ -743,3 +743,20 @@ func TestReportCallerOnTextFormatter(t *testing.T) {
l.Formatter.(*TextFormatter).DisableColors = true
l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")
}
func TestSetReportCallerRace(t *testing.T) {
l := New()
l.Out = ioutil.Discard
l.SetReportCaller(true)
var wg sync.WaitGroup
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
l.Error("Some Error")
wg.Done()
}()
}
wg.Wait()
}