2014-11-06 04:39:35 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2015-05-13 14:35:03 +03:00
|
|
|
func TestEntryWithError(t *testing.T) {
|
|
|
|
|
2015-05-19 20:50:55 +03:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ErrorKey = "error"
|
|
|
|
}()
|
|
|
|
|
2015-09-08 03:47:46 +03:00
|
|
|
err := fmt.Errorf("kaboom at layer %d", 4711)
|
|
|
|
|
2015-05-19 20:50:55 +03:00
|
|
|
assert.Equal(err, WithError(err).Data["error"])
|
|
|
|
|
2015-05-13 14:35:03 +03:00
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
|
2015-05-19 20:50:55 +03:00
|
|
|
assert.Equal(err, entry.WithError(err).Data["error"])
|
2015-05-13 14:35:03 +03:00
|
|
|
|
|
|
|
ErrorKey = "err"
|
2015-05-19 20:50:55 +03:00
|
|
|
|
|
|
|
assert.Equal(err, entry.WithError(err).Data["err"])
|
2015-05-13 14:35:03 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-06 04:39:35 +03:00
|
|
|
func TestEntryPanicln(t *testing.T) {
|
|
|
|
errBoom := fmt.Errorf("boom time")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
|
|
|
|
switch pVal := p.(type) {
|
|
|
|
case *Entry:
|
|
|
|
assert.Equal(t, "kaboom", pVal.Message)
|
|
|
|
assert.Equal(t, errBoom, pVal.Data["err"])
|
|
|
|
default:
|
2014-11-06 16:42:52 +03:00
|
|
|
t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
|
2014-11-06 04:39:35 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
entry.WithField("err", errBoom).Panicln("kaboom")
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2014-11-06 16:42:52 +03:00
|
|
|
t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
|
2014-11-06 04:39:35 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
entry.WithField("err", errBoom).Panicf("kaboom %v", true)
|
|
|
|
}
|
2018-01-22 18:35:26 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
badMessage = "this is going to panic"
|
|
|
|
panicMessage = "this is broken"
|
|
|
|
)
|
|
|
|
|
|
|
|
type panickyHook struct{}
|
|
|
|
|
|
|
|
func (p *panickyHook) Levels() []Level {
|
|
|
|
return []Level{InfoLevel}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *panickyHook) Fire(entry *Entry) error {
|
|
|
|
if entry.Message == badMessage {
|
|
|
|
panic(panicMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEntryHooksPanic(t *testing.T) {
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
logger.Level = InfoLevel
|
|
|
|
logger.Hooks.Add(&panickyHook{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
assert.Equal(t, panicMessage, p)
|
|
|
|
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
entry.Info("another message")
|
|
|
|
}()
|
|
|
|
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
entry.Info(badMessage)
|
|
|
|
}
|
2018-12-05 21:14:19 +03:00
|
|
|
|
|
|
|
func TestEntryWithIncorrectField(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
fn := func() {}
|
|
|
|
|
|
|
|
e := Entry{}
|
|
|
|
eWithFunc := e.WithFields(Fields{"func": fn})
|
|
|
|
eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
|
|
|
|
|
|
|
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
|
|
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
|
|
|
}
|