2014-11-06 04:39:35 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-11 13:47:03 +03:00
|
|
|
"context"
|
2014-11-06 04:39:35 +03:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2018-12-14 19:01:34 +03:00
|
|
|
"time"
|
2014-11-06 04:39:35 +03:00
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:47:03 +03:00
|
|
|
func TestEntryWithContext(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
ctx := context.WithValue(context.Background(), "foo", "bar")
|
|
|
|
|
|
|
|
assert.Equal(ctx, WithContext(ctx).Context)
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
|
|
|
|
assert.Equal(ctx, entry.WithContext(ctx).Context)
|
|
|
|
}
|
|
|
|
|
2019-11-28 07:03:38 +03:00
|
|
|
func TestEntryWithContextCopiesData(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
ctx1 := context.WithValue(context.Background(), "foo", "bar")
|
|
|
|
ctx2 := context.WithValue(context.Background(), "bar", "baz")
|
|
|
|
assert.NotEqual(ctx1, ctx2)
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &bytes.Buffer{}
|
|
|
|
parentEntry := NewEntry(logger).WithField("parentKey", "parentValue")
|
|
|
|
childEntry1 := parentEntry.WithContext(ctx1)
|
|
|
|
assert.Equal(ctx1, childEntry1.Context)
|
|
|
|
childEntry2 := parentEntry.WithContext(ctx2)
|
|
|
|
assert.Equal(ctx2, childEntry2.Context)
|
|
|
|
assert.NotEqual(ctx1, ctx2)
|
|
|
|
assert.Equal("parentValue", childEntry1.Data["parentKey"])
|
|
|
|
assert.Equal("parentValue", childEntry2.Data["parentKey"])
|
|
|
|
|
|
|
|
childEntry1.Data["ChildKeyValue1"] = "ChildDataValue1"
|
|
|
|
|
|
|
|
val, exists := childEntry1.Data["ChildKeyValue1"]
|
|
|
|
assert.True(exists)
|
|
|
|
assert.Equal("ChildDataValue1", val)
|
|
|
|
|
|
|
|
val, exists = childEntry2.Data["ChildKeyValue1"]
|
|
|
|
assert.False(exists)
|
|
|
|
assert.Empty(val)
|
|
|
|
}
|
|
|
|
|
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"`)
|
2018-12-14 19:01:34 +03:00
|
|
|
|
|
|
|
eWithFunc = eWithFunc.WithField("not_a_func", "it is a string")
|
|
|
|
eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string")
|
|
|
|
|
|
|
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
|
|
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
|
|
|
|
|
|
|
eWithFunc = eWithFunc.WithTime(time.Now())
|
|
|
|
eWithFuncPtr = eWithFuncPtr.WithTime(time.Now())
|
|
|
|
|
|
|
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
|
|
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
2018-12-05 21:14:19 +03:00
|
|
|
}
|
2019-02-06 22:51:07 +03:00
|
|
|
|
|
|
|
func TestEntryLogfLevel(t *testing.T) {
|
|
|
|
logger := New()
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
logger.Out = buffer
|
|
|
|
logger.SetLevel(InfoLevel)
|
|
|
|
entry := NewEntry(logger)
|
|
|
|
|
|
|
|
entry.Logf(DebugLevel, "%s", "debug")
|
|
|
|
assert.NotContains(t, buffer.String(), "debug", )
|
|
|
|
|
|
|
|
entry.Logf(WarnLevel, "%s", "warn")
|
|
|
|
assert.Contains(t, buffer.String(), "warn", )
|
|
|
|
}
|