2014-11-06 04:39:35 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-07-06 23:27:39 +03:00
|
|
|
|
|
|
|
func TestEntryLogLevel(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
logger := New()
|
|
|
|
logger.Out = out
|
|
|
|
logger.Level = DebugLevel
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
assert.Equal(t, DebugLevel, entry.Level)
|
|
|
|
entry.Level = WarnLevel
|
|
|
|
entry.Info("it should not be displayed")
|
|
|
|
assert.Equal(t, "", out.String())
|
|
|
|
entry.Warn("it should be displayed")
|
|
|
|
assert.Contains(t, out.String(), "it should be displayed")
|
|
|
|
}
|