123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package logrus
-
- import (
- "sync"
- "testing"
-
- "github.com/stretchr/testify/assert"
- )
-
- type TestHook struct {
- Fired bool
- }
-
- func (hook *TestHook) Fire(entry *Entry) error {
- hook.Fired = true
- return nil
- }
-
- func (hook *TestHook) Levels() []Level {
- return []Level{
- DebugLevel,
- InfoLevel,
- WarnLevel,
- ErrorLevel,
- FatalLevel,
- PanicLevel,
- }
- }
-
- func TestHookFires(t *testing.T) {
- hook := new(TestHook)
-
- LogAndAssertJSON(t, func(log *Logger) {
- log.Hooks.Add(hook)
- assert.Equal(t, hook.Fired, false)
-
- log.Print("test")
- }, func(fields Fields) {
- assert.Equal(t, hook.Fired, true)
- })
- }
-
- type ModifyHook struct {
- }
-
- func (hook *ModifyHook) Fire(entry *Entry) error {
- entry.Data["wow"] = "whale"
- return nil
- }
-
- func (hook *ModifyHook) Levels() []Level {
- return []Level{
- DebugLevel,
- InfoLevel,
- WarnLevel,
- ErrorLevel,
- FatalLevel,
- PanicLevel,
- }
- }
-
- func TestHookCanModifyEntry(t *testing.T) {
- hook := new(ModifyHook)
-
- LogAndAssertJSON(t, func(log *Logger) {
- log.Hooks.Add(hook)
- log.WithField("wow", "elephant").Print("test")
- }, func(fields Fields) {
- assert.Equal(t, fields["wow"], "whale")
- })
- }
-
- func TestCanFireMultipleHooks(t *testing.T) {
- hook1 := new(ModifyHook)
- hook2 := new(TestHook)
-
- LogAndAssertJSON(t, func(log *Logger) {
- log.Hooks.Add(hook1)
- log.Hooks.Add(hook2)
-
- log.WithField("wow", "elephant").Print("test")
- }, func(fields Fields) {
- assert.Equal(t, fields["wow"], "whale")
- assert.Equal(t, hook2.Fired, true)
- })
- }
-
- type ErrorHook struct {
- Fired bool
- }
-
- func (hook *ErrorHook) Fire(entry *Entry) error {
- hook.Fired = true
- return nil
- }
-
- func (hook *ErrorHook) Levels() []Level {
- return []Level{
- ErrorLevel,
- }
- }
-
- func TestErrorHookShouldntFireOnInfo(t *testing.T) {
- hook := new(ErrorHook)
-
- LogAndAssertJSON(t, func(log *Logger) {
- log.Hooks.Add(hook)
- log.Info("test")
- }, func(fields Fields) {
- assert.Equal(t, hook.Fired, false)
- })
- }
-
- func TestErrorHookShouldFireOnError(t *testing.T) {
- hook := new(ErrorHook)
-
- LogAndAssertJSON(t, func(log *Logger) {
- log.Hooks.Add(hook)
- log.Error("test")
- }, func(fields Fields) {
- assert.Equal(t, hook.Fired, true)
- })
- }
-
- func TestAddHookRace(t *testing.T) {
- var wg sync.WaitGroup
- wg.Add(2)
- hook := new(ErrorHook)
- LogAndAssertJSON(t, func(log *Logger) {
- go func() {
- defer wg.Done()
- log.AddHook(hook)
- }()
- go func() {
- defer wg.Done()
- log.Error("test")
- }()
- wg.Wait()
- }, func(fields Fields) {
- // the line may have been logged
- // before the hook was added, so we can't
- // actually assert on the hook
- })
- }
|