mirror of https://github.com/sirupsen/logrus.git
Merge pull request #1234 from sirupsen/dbd-cleanup
fix race condition AddHook and traces
This commit is contained in:
commit
6cff360233
10
entry.go
10
entry.go
|
@ -261,7 +261,15 @@ func (entry *Entry) log(level Level, msg string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) fireHooks() {
|
func (entry *Entry) fireHooks() {
|
||||||
err := entry.Logger.Hooks.Fire(entry.Level, entry)
|
var tmpHooks LevelHooks
|
||||||
|
entry.Logger.mu.Lock()
|
||||||
|
tmpHooks = make(LevelHooks, len(entry.Logger.Hooks))
|
||||||
|
for k, v := range entry.Logger.Hooks {
|
||||||
|
tmpHooks[k] = v
|
||||||
|
}
|
||||||
|
entry.Logger.mu.Unlock()
|
||||||
|
|
||||||
|
err := tmpHooks.Fire(entry.Level, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
16
hook_test.go
16
hook_test.go
|
@ -3,6 +3,7 @@ package logrus_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
. "github.com/sirupsen/logrus"
|
. "github.com/sirupsen/logrus"
|
||||||
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
. "github.com/sirupsen/logrus/internal/testutils"
|
. "github.com/sirupsen/logrus/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -191,6 +193,20 @@ func TestAddHookRace(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddHookRace2(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
testname := fmt.Sprintf("Test %d", i)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
_ = test.NewGlobal()
|
||||||
|
Info(testname)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type HookCallFunc struct {
|
type HookCallFunc struct {
|
||||||
F func()
|
F func()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue