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) }) }
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) }) }
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 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 TestEntryPanicf(t *testing.T) { errBoom := fmt.Errorf("boom again") defer func() { p := recover() assert.NotNil(t, p) switch pVal := p.(type) { case *Entry: assert.Equal(t, "kaboom true", pVal.Message) assert.Equal(t, errBoom, pVal.Data["err"]) default: t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal) } }() logger := New() logger.Out = &bytes.Buffer{} entry := NewEntry(logger) entry.WithField("err", errBoom).Panicf("kaboom %v", true) }