2018-09-30 23:51:02 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-22 15:08:19 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-30 23:51:02 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFieldValueError(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
l := &Logger{
|
|
|
|
Out: buf,
|
|
|
|
Formatter: new(JSONFormatter),
|
|
|
|
Hooks: make(LevelHooks),
|
|
|
|
Level: DebugLevel,
|
|
|
|
}
|
|
|
|
l.WithField("func", func() {}).Info("test")
|
|
|
|
fmt.Println(buf.String())
|
|
|
|
var data map[string]interface{}
|
2019-10-14 03:45:01 +03:00
|
|
|
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
|
|
|
|
t.Error("unexpected error", err)
|
|
|
|
}
|
2018-09-30 23:51:02 +03:00
|
|
|
_, ok := data[FieldKeyLogrusError]
|
2021-02-16 12:29:37 +03:00
|
|
|
require.True(t, ok, `cannot found expected "logrus_error" field: %v`, data)
|
2018-09-30 23:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoFieldValueError(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
l := &Logger{
|
|
|
|
Out: buf,
|
|
|
|
Formatter: new(JSONFormatter),
|
|
|
|
Hooks: make(LevelHooks),
|
|
|
|
Level: DebugLevel,
|
|
|
|
}
|
|
|
|
l.WithField("str", "str").Info("test")
|
|
|
|
fmt.Println(buf.String())
|
|
|
|
var data map[string]interface{}
|
2019-10-14 03:45:01 +03:00
|
|
|
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
|
|
|
|
t.Error("unexpected error", err)
|
|
|
|
}
|
2018-09-30 23:51:02 +03:00
|
|
|
_, ok := data[FieldKeyLogrusError]
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
2019-01-22 15:08:19 +03:00
|
|
|
|
|
|
|
func TestWarninglnNotEqualToWarning(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
bufln := &bytes.Buffer{}
|
|
|
|
|
|
|
|
formatter := new(TextFormatter)
|
|
|
|
formatter.DisableTimestamp = true
|
|
|
|
formatter.DisableLevelTruncation = true
|
|
|
|
|
|
|
|
l := &Logger{
|
|
|
|
Out: buf,
|
|
|
|
Formatter: formatter,
|
|
|
|
Hooks: make(LevelHooks),
|
|
|
|
Level: DebugLevel,
|
|
|
|
}
|
|
|
|
l.Warning("hello,", "world")
|
|
|
|
|
|
|
|
l.SetOutput(bufln)
|
|
|
|
l.Warningln("hello,", "world")
|
|
|
|
|
|
|
|
assert.NotEqual(t, buf.String(), bufln.String(), "Warning() and Wantingln() should not be equal")
|
|
|
|
}
|
2021-04-20 05:48:30 +03:00
|
|
|
|
|
|
|
type testBufferPool struct {
|
|
|
|
buffers []*bytes.Buffer
|
|
|
|
get int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testBufferPool) Get() *bytes.Buffer {
|
|
|
|
p.get++
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testBufferPool) Put(buf *bytes.Buffer) {
|
|
|
|
p.buffers = append(p.buffers, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetBufferPool(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
l := New()
|
|
|
|
l.SetOutput(out)
|
|
|
|
|
|
|
|
pool := new(testBufferPool)
|
|
|
|
l.SetBufferPool(pool)
|
|
|
|
|
|
|
|
l.Info("test")
|
|
|
|
|
|
|
|
assert.Equal(t, pool.get, 1, "Logger.SetBufferPool(): The BufferPool.Get() must be called")
|
|
|
|
assert.Len(t, pool.buffers, 1, "Logger.SetBufferPool(): The BufferPool.Put() must be called")
|
|
|
|
}
|