mirror of https://github.com/sirupsen/logrus.git
parent
a4096716b0
commit
179037fcd4
43
hook_test.go
43
hook_test.go
|
@ -1,10 +1,13 @@
|
|||
package logrus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type TestHook struct {
|
||||
|
@ -85,6 +88,46 @@ func TestCanFireMultipleHooks(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
type SingleLevelModifyHook struct {
|
||||
ModifyHook
|
||||
}
|
||||
|
||||
func (h *SingleLevelModifyHook) Levels() []Level {
|
||||
return []Level{InfoLevel}
|
||||
}
|
||||
|
||||
func TestHookEntryIsPristine(t *testing.T) {
|
||||
l := New()
|
||||
b := &bytes.Buffer{}
|
||||
l.Formatter = &JSONFormatter{}
|
||||
l.Out = b
|
||||
l.AddHook(&SingleLevelModifyHook{})
|
||||
|
||||
l.Error("error message")
|
||||
data := map[string]string{}
|
||||
err := json.Unmarshal(b.Bytes(), &data)
|
||||
require.NoError(t, err)
|
||||
_, ok := data["wow"]
|
||||
require.False(t, ok)
|
||||
b.Reset()
|
||||
|
||||
l.Info("error message")
|
||||
data = map[string]string{}
|
||||
err = json.Unmarshal(b.Bytes(), &data)
|
||||
require.NoError(t, err)
|
||||
_, ok = data["wow"]
|
||||
require.True(t, ok)
|
||||
b.Reset()
|
||||
|
||||
l.Error("error message")
|
||||
data = map[string]string{}
|
||||
err = json.Unmarshal(b.Bytes(), &data)
|
||||
require.NoError(t, err)
|
||||
_, ok = data["wow"]
|
||||
require.False(t, ok)
|
||||
b.Reset()
|
||||
}
|
||||
|
||||
type ErrorHook struct {
|
||||
Fired bool
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue