2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2017-05-12 05:24:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-05-17 05:21:18 +00:00
|
|
|
"errors"
|
2018-02-07 21:54:26 +00:00
|
|
|
"fmt"
|
2018-04-03 21:07:18 +00:00
|
|
|
"net"
|
2017-05-12 05:24:39 +00:00
|
|
|
"reflect"
|
2018-02-07 21:54:26 +00:00
|
|
|
"runtime"
|
2019-02-20 19:39:29 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-05-12 05:24:39 +00:00
|
|
|
"testing"
|
2017-05-17 05:21:18 +00:00
|
|
|
"time"
|
2017-05-12 05:24:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLog(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), "{}\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("one-field", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Str("foo", "bar").Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("two-field", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().
|
|
|
|
Str("foo", "bar").
|
|
|
|
Int("n", 123).
|
|
|
|
Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","n":123}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Info().Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"info"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("one-field", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Info().Str("foo", "bar").Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"info","foo":"bar"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("two-field", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Info().
|
|
|
|
Str("foo", "bar").
|
|
|
|
Int("n", 123).
|
|
|
|
Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"info","foo":"bar","n":123}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-05 12:40:45 +00:00
|
|
|
func TestEmptyLevelFieldName(t *testing.T) {
|
|
|
|
fieldName := LevelFieldName
|
|
|
|
LevelFieldName = ""
|
|
|
|
|
|
|
|
t.Run("empty setting", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Info().
|
|
|
|
Str("foo", "bar").
|
|
|
|
Int("n", 123).
|
|
|
|
Msg("")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","n":123}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
LevelFieldName = fieldName
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
func TestWith(t *testing.T) {
|
2017-05-17 05:21:18 +00:00
|
|
|
out := &bytes.Buffer{}
|
2018-02-07 21:54:26 +00:00
|
|
|
ctx := New(out).With().
|
2018-03-15 17:29:26 +00:00
|
|
|
Str("string", "foo").
|
2020-06-18 03:42:03 +00:00
|
|
|
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
|
|
|
Stringer("stringer_nil", nil).
|
2018-03-15 17:29:26 +00:00
|
|
|
Bytes("bytes", []byte("bar")).
|
|
|
|
Hex("hex", []byte{0x12, 0xef}).
|
2018-02-13 00:05:27 +00:00
|
|
|
RawJSON("json", []byte(`{"some":"json"}`)).
|
2017-06-02 07:56:14 +00:00
|
|
|
AnErr("some_err", nil).
|
2017-05-17 05:21:18 +00:00
|
|
|
Err(errors.New("some error")).
|
|
|
|
Bool("bool", true).
|
|
|
|
Int("int", 1).
|
|
|
|
Int8("int8", 2).
|
|
|
|
Int16("int16", 3).
|
|
|
|
Int32("int32", 4).
|
|
|
|
Int64("int64", 5).
|
|
|
|
Uint("uint", 6).
|
|
|
|
Uint8("uint8", 7).
|
|
|
|
Uint16("uint16", 8).
|
|
|
|
Uint32("uint32", 9).
|
2017-05-18 07:10:45 +00:00
|
|
|
Uint64("uint64", 10).
|
2018-04-13 07:13:41 +00:00
|
|
|
Float32("float32", 11.101).
|
|
|
|
Float64("float64", 12.30303).
|
2018-02-07 21:54:26 +00:00
|
|
|
Time("time", time.Time{})
|
|
|
|
_, file, line, _ := runtime.Caller(0)
|
|
|
|
caller := fmt.Sprintf("%s:%d", file, line+3)
|
|
|
|
log := ctx.Caller().Logger()
|
2017-05-17 05:21:18 +00:00
|
|
|
log.Log().Msg("")
|
2020-06-18 03:42:03 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
2019-03-05 00:41:18 +00:00
|
|
|
|
|
|
|
// Validate CallerWithSkipFrameCount.
|
|
|
|
out.Reset()
|
|
|
|
_, file, line, _ = runtime.Caller(0)
|
|
|
|
caller = fmt.Sprintf("%s:%d", file, line+5)
|
|
|
|
log = ctx.CallerWithSkipFrameCount(3).Logger()
|
|
|
|
func() {
|
|
|
|
log.Log().Msg("")
|
|
|
|
}()
|
|
|
|
// The above line is a little contrived, but the line above should be the line due
|
|
|
|
// to the extra frame skip.
|
2020-06-18 03:42:03 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
|
2019-03-05 00:41:18 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
2017-07-10 09:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsMap(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Fields(map[string]interface{}{
|
|
|
|
"nil": nil,
|
|
|
|
"string": "foo",
|
|
|
|
"bytes": []byte("bar"),
|
|
|
|
"error": errors.New("some error"),
|
|
|
|
"bool": true,
|
|
|
|
"int": int(1),
|
|
|
|
"int8": int8(2),
|
|
|
|
"int16": int16(3),
|
|
|
|
"int32": int32(4),
|
|
|
|
"int64": int64(5),
|
|
|
|
"uint": uint(6),
|
|
|
|
"uint8": uint8(7),
|
|
|
|
"uint16": uint16(8),
|
|
|
|
"uint32": uint32(9),
|
|
|
|
"uint64": uint64(10),
|
|
|
|
"float32": float32(11),
|
|
|
|
"float64": float64(12),
|
2018-04-03 21:07:18 +00:00
|
|
|
"ipv6": net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
|
2017-07-10 09:56:44 +00:00
|
|
|
"dur": 1 * time.Second,
|
|
|
|
"time": time.Time{},
|
2018-05-09 10:51:52 +00:00
|
|
|
"obj": obj{"a", "b", 1},
|
2017-07-10 09:56:44 +00:00
|
|
|
}).Msg("")
|
2018-05-09 10:51:52 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"ipv6":"2001:db8:85a3::8a2e:370:7334","nil":null,"obj":{"Pub":"a","Tag":"b","priv":1},"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-17 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 03:18:47 +00:00
|
|
|
func TestFieldsMapPnt(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Fields(map[string]interface{}{
|
|
|
|
"string": new(string),
|
|
|
|
"bool": new(bool),
|
|
|
|
"int": new(int),
|
|
|
|
"int8": new(int8),
|
|
|
|
"int16": new(int16),
|
|
|
|
"int32": new(int32),
|
|
|
|
"int64": new(int64),
|
|
|
|
"uint": new(uint),
|
|
|
|
"uint8": new(uint8),
|
|
|
|
"uint16": new(uint16),
|
|
|
|
"uint32": new(uint32),
|
|
|
|
"uint64": new(uint64),
|
|
|
|
"float32": new(float32),
|
|
|
|
"float64": new(float64),
|
|
|
|
"dur": new(time.Duration),
|
|
|
|
"time": new(time.Time),
|
|
|
|
}).Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":false,"dur":0,"float32":0,"float64":0,"int":0,"int16":0,"int32":0,"int64":0,"int8":0,"string":"","time":"0001-01-01T00:00:00Z","uint":0,"uint16":0,"uint32":0,"uint64":0,"uint8":0}`+"\n"; got != want {
|
2018-03-26 03:18:47 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 17:40:46 +00:00
|
|
|
func TestFieldsMapNilPnt(t *testing.T) {
|
|
|
|
var (
|
|
|
|
stringPnt *string
|
|
|
|
boolPnt *bool
|
|
|
|
intPnt *int
|
|
|
|
int8Pnt *int8
|
|
|
|
int16Pnt *int16
|
|
|
|
int32Pnt *int32
|
|
|
|
int64Pnt *int64
|
|
|
|
uintPnt *uint
|
|
|
|
uint8Pnt *uint8
|
|
|
|
uint16Pnt *uint16
|
|
|
|
uint32Pnt *uint32
|
|
|
|
uint64Pnt *uint64
|
|
|
|
float32Pnt *float32
|
|
|
|
float64Pnt *float64
|
|
|
|
durPnt *time.Duration
|
|
|
|
timePnt *time.Time
|
|
|
|
)
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"string": stringPnt,
|
|
|
|
"bool": boolPnt,
|
|
|
|
"int": intPnt,
|
|
|
|
"int8": int8Pnt,
|
|
|
|
"int16": int16Pnt,
|
|
|
|
"int32": int32Pnt,
|
|
|
|
"int64": int64Pnt,
|
|
|
|
"uint": uintPnt,
|
|
|
|
"uint8": uint8Pnt,
|
|
|
|
"uint16": uint16Pnt,
|
|
|
|
"uint32": uint32Pnt,
|
|
|
|
"uint64": uint64Pnt,
|
|
|
|
"float32": float32Pnt,
|
|
|
|
"float64": float64Pnt,
|
|
|
|
"dur": durPnt,
|
|
|
|
"time": timePnt,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Log().Fields(fields).Msg("")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":null,"dur":null,"float32":null,"float64":null,"int":null,"int16":null,"int32":null,"int64":null,"int8":null,"string":null,"time":null,"uint":null,"uint16":null,"uint32":null,"uint64":null,"uint8":null}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 16:00:06 +00:00
|
|
|
func TestFieldsSlice(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Fields([]interface{}{
|
|
|
|
"nil", nil,
|
|
|
|
"string", "foo",
|
|
|
|
"bytes", []byte("bar"),
|
|
|
|
"error", errors.New("some error"),
|
|
|
|
"bool", true,
|
|
|
|
"int", int(1),
|
|
|
|
"int8", int8(2),
|
|
|
|
"int16", int16(3),
|
|
|
|
"int32", int32(4),
|
|
|
|
"int64", int64(5),
|
|
|
|
"uint", uint(6),
|
|
|
|
"uint8", uint8(7),
|
|
|
|
"uint16", uint16(8),
|
|
|
|
"uint32", uint32(9),
|
|
|
|
"uint64", uint64(10),
|
|
|
|
"float32", float32(11),
|
|
|
|
"float64", float64(12),
|
|
|
|
"ipv6", net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
|
|
|
|
"dur", 1 * time.Second,
|
|
|
|
"time", time.Time{},
|
|
|
|
"obj", obj{"a", "b", 1},
|
|
|
|
}).Msg("")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"nil":null,"string":"foo","bytes":"bar","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"ipv6":"2001:db8:85a3::8a2e:370:7334","dur":1000,"time":"0001-01-01T00:00:00Z","obj":{"Pub":"a","Tag":"b","priv":1}}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsSliceExtraneous(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Fields([]interface{}{
|
|
|
|
"string", "foo",
|
|
|
|
"error", errors.New("some error"),
|
|
|
|
32, "valueForNonStringKey",
|
|
|
|
"bool", true,
|
|
|
|
"int", int(1),
|
|
|
|
"keyWithoutValue",
|
|
|
|
}).Msg("")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","error":"some error","bool":true,"int":1}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsNotMapSlice(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().
|
|
|
|
Fields(obj{"a", "b", 1}).
|
|
|
|
Fields("string").
|
|
|
|
Fields(1).
|
|
|
|
Msg("")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:21:18 +00:00
|
|
|
func TestFields(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
2017-06-25 08:12:41 +00:00
|
|
|
now := time.Now()
|
2018-02-07 21:54:26 +00:00
|
|
|
_, file, line, _ := runtime.Caller(0)
|
|
|
|
caller := fmt.Sprintf("%s:%d", file, line+3)
|
2017-05-17 05:21:18 +00:00
|
|
|
log.Log().
|
2018-02-07 21:54:26 +00:00
|
|
|
Caller().
|
2017-07-10 09:56:44 +00:00
|
|
|
Str("string", "foo").
|
2020-05-28 17:43:18 +00:00
|
|
|
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
|
|
|
Stringer("stringer_nil", nil).
|
2017-07-10 09:56:44 +00:00
|
|
|
Bytes("bytes", []byte("bar")).
|
2018-03-15 17:29:26 +00:00
|
|
|
Hex("hex", []byte{0x12, 0xef}).
|
2018-02-13 00:05:27 +00:00
|
|
|
RawJSON("json", []byte(`{"some":"json"}`)).
|
2021-05-19 23:33:11 +00:00
|
|
|
Func(func(e *Event) { e.Str("func", "func_output") }).
|
2017-06-02 07:56:14 +00:00
|
|
|
AnErr("some_err", nil).
|
2017-05-17 05:21:18 +00:00
|
|
|
Err(errors.New("some error")).
|
|
|
|
Bool("bool", true).
|
|
|
|
Int("int", 1).
|
|
|
|
Int8("int8", 2).
|
|
|
|
Int16("int16", 3).
|
|
|
|
Int32("int32", 4).
|
|
|
|
Int64("int64", 5).
|
|
|
|
Uint("uint", 6).
|
|
|
|
Uint8("uint8", 7).
|
|
|
|
Uint16("uint16", 8).
|
|
|
|
Uint32("uint32", 9).
|
2017-05-18 07:10:45 +00:00
|
|
|
Uint64("uint64", 10).
|
2018-04-03 21:07:18 +00:00
|
|
|
IPAddr("IPv4", net.IP{192, 168, 0, 100}).
|
|
|
|
IPAddr("IPv6", net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}).
|
|
|
|
MACAddr("Mac", net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}).
|
|
|
|
IPPrefix("Prefix", net.IPNet{IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}).
|
2018-04-13 07:13:41 +00:00
|
|
|
Float32("float32", 11.1234).
|
|
|
|
Float64("float64", 12.321321321).
|
2017-06-07 04:58:33 +00:00
|
|
|
Dur("dur", 1*time.Second).
|
2017-05-17 05:21:18 +00:00
|
|
|
Time("time", time.Time{}).
|
2017-06-25 08:12:41 +00:00
|
|
|
TimeDiff("diff", now, now.Add(-10*time.Second)).
|
2017-05-17 05:21:18 +00:00
|
|
|
Msg("")
|
2021-05-19 23:33:11 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"func":"func_output","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"IPv4":"192.168.0.100","IPv6":"2001:db8:85a3::8a2e:370:7334","Mac":"00:14:22:01:23:45","Prefix":"192.168.0.100/24","float32":11.1234,"float64":12.321321321,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-17 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
func TestFieldsArrayEmpty(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().
|
|
|
|
Strs("string", []string{}).
|
2021-09-18 17:22:21 +00:00
|
|
|
Stringers("stringer", []fmt.Stringer{}).
|
2017-07-25 19:50:35 +00:00
|
|
|
Errs("err", []error{}).
|
|
|
|
Bools("bool", []bool{}).
|
|
|
|
Ints("int", []int{}).
|
|
|
|
Ints8("int8", []int8{}).
|
|
|
|
Ints16("int16", []int16{}).
|
|
|
|
Ints32("int32", []int32{}).
|
|
|
|
Ints64("int64", []int64{}).
|
|
|
|
Uints("uint", []uint{}).
|
|
|
|
Uints8("uint8", []uint8{}).
|
|
|
|
Uints16("uint16", []uint16{}).
|
|
|
|
Uints32("uint32", []uint32{}).
|
|
|
|
Uints64("uint64", []uint64{}).
|
|
|
|
Floats32("float32", []float32{}).
|
|
|
|
Floats64("float64", []float64{}).
|
|
|
|
Durs("dur", []time.Duration{}).
|
|
|
|
Times("time", []time.Time{}).
|
|
|
|
Msg("")
|
2021-09-18 17:22:21 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":[],"stringer":[],"err":[],"bool":[],"int":[],"int8":[],"int16":[],"int32":[],"int64":[],"uint":[],"uint8":[],"uint16":[],"uint32":[],"uint64":[],"float32":[],"float64":[],"dur":[],"time":[]}`+"\n"; got != want {
|
2017-07-25 19:50:35 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsArraySingleElement(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().
|
|
|
|
Strs("string", []string{"foo"}).
|
2021-09-18 17:22:21 +00:00
|
|
|
Stringers("stringer", []fmt.Stringer{net.IP{127, 0, 0, 1}}).
|
2017-07-25 19:50:35 +00:00
|
|
|
Errs("err", []error{errors.New("some error")}).
|
|
|
|
Bools("bool", []bool{true}).
|
|
|
|
Ints("int", []int{1}).
|
|
|
|
Ints8("int8", []int8{2}).
|
|
|
|
Ints16("int16", []int16{3}).
|
|
|
|
Ints32("int32", []int32{4}).
|
|
|
|
Ints64("int64", []int64{5}).
|
|
|
|
Uints("uint", []uint{6}).
|
|
|
|
Uints8("uint8", []uint8{7}).
|
|
|
|
Uints16("uint16", []uint16{8}).
|
|
|
|
Uints32("uint32", []uint32{9}).
|
|
|
|
Uints64("uint64", []uint64{10}).
|
|
|
|
Floats32("float32", []float32{11}).
|
|
|
|
Floats64("float64", []float64{12}).
|
|
|
|
Durs("dur", []time.Duration{1 * time.Second}).
|
2021-09-08 16:00:06 +00:00
|
|
|
Times("time", []time.Time{{}}).
|
2017-07-25 19:50:35 +00:00
|
|
|
Msg("")
|
2021-09-18 17:22:21 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo"],"stringer":["127.0.0.1"],"err":["some error"],"bool":[true],"int":[1],"int8":[2],"int16":[3],"int32":[4],"int64":[5],"uint":[6],"uint8":[7],"uint16":[8],"uint32":[9],"uint64":[10],"float32":[11],"float64":[12],"dur":[1000],"time":["0001-01-01T00:00:00Z"]}`+"\n"; got != want {
|
2017-07-25 19:50:35 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsArrayMultipleElement(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().
|
|
|
|
Strs("string", []string{"foo", "bar"}).
|
2021-09-18 17:22:21 +00:00
|
|
|
Stringers("stringer", []fmt.Stringer{nil, net.IP{127, 0, 0, 1}}).
|
2017-07-25 19:50:35 +00:00
|
|
|
Errs("err", []error{errors.New("some error"), nil}).
|
|
|
|
Bools("bool", []bool{true, false}).
|
|
|
|
Ints("int", []int{1, 0}).
|
|
|
|
Ints8("int8", []int8{2, 0}).
|
|
|
|
Ints16("int16", []int16{3, 0}).
|
|
|
|
Ints32("int32", []int32{4, 0}).
|
|
|
|
Ints64("int64", []int64{5, 0}).
|
|
|
|
Uints("uint", []uint{6, 0}).
|
|
|
|
Uints8("uint8", []uint8{7, 0}).
|
|
|
|
Uints16("uint16", []uint16{8, 0}).
|
|
|
|
Uints32("uint32", []uint32{9, 0}).
|
|
|
|
Uints64("uint64", []uint64{10, 0}).
|
|
|
|
Floats32("float32", []float32{11, 0}).
|
|
|
|
Floats64("float64", []float64{12, 0}).
|
|
|
|
Durs("dur", []time.Duration{1 * time.Second, 0}).
|
2021-09-08 16:00:06 +00:00
|
|
|
Times("time", []time.Time{{}, {}}).
|
2017-07-25 19:50:35 +00:00
|
|
|
Msg("")
|
2021-09-18 17:22:21 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo","bar"],"stringer":[null,"127.0.0.1"],"err":["some error",null],"bool":[true,false],"int":[1,0],"int8":[2,0],"int16":[3,0],"int32":[4,0],"int64":[5,0],"uint":[6,0],"uint8":[7,0],"uint16":[8,0],"uint32":[9,0],"uint64":[10,0],"float32":[11,0],"float64":[12,0],"dur":[1000,0],"time":["0001-01-01T00:00:00Z","0001-01-01T00:00:00Z"]}`+"\n"; got != want {
|
2017-07-25 19:50:35 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:21:18 +00:00
|
|
|
func TestFieldsDisabled(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(InfoLevel)
|
2017-06-25 08:12:41 +00:00
|
|
|
now := time.Now()
|
2017-05-17 05:21:18 +00:00
|
|
|
log.Debug().
|
2017-07-10 09:56:44 +00:00
|
|
|
Str("string", "foo").
|
2021-09-18 17:22:21 +00:00
|
|
|
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
2017-07-10 09:56:44 +00:00
|
|
|
Bytes("bytes", []byte("bar")).
|
2018-03-15 17:29:26 +00:00
|
|
|
Hex("hex", []byte{0x12, 0xef}).
|
2017-06-02 07:56:14 +00:00
|
|
|
AnErr("some_err", nil).
|
2017-05-17 05:21:18 +00:00
|
|
|
Err(errors.New("some error")).
|
2021-05-19 23:33:11 +00:00
|
|
|
Func(func(e *Event) { e.Str("func", "func_output") }).
|
2017-05-17 05:21:18 +00:00
|
|
|
Bool("bool", true).
|
|
|
|
Int("int", 1).
|
|
|
|
Int8("int8", 2).
|
|
|
|
Int16("int16", 3).
|
|
|
|
Int32("int32", 4).
|
|
|
|
Int64("int64", 5).
|
|
|
|
Uint("uint", 6).
|
|
|
|
Uint8("uint8", 7).
|
|
|
|
Uint16("uint16", 8).
|
|
|
|
Uint32("uint32", 9).
|
2017-05-18 07:10:45 +00:00
|
|
|
Uint64("uint64", 10).
|
2017-05-17 05:21:18 +00:00
|
|
|
Float32("float32", 11).
|
|
|
|
Float64("float64", 12).
|
2017-06-07 04:58:33 +00:00
|
|
|
Dur("dur", 1*time.Second).
|
2017-05-17 05:21:18 +00:00
|
|
|
Time("time", time.Time{}).
|
2017-06-25 08:12:41 +00:00
|
|
|
TimeDiff("diff", now, now.Add(-10*time.Second)).
|
2017-05-17 05:21:18 +00:00
|
|
|
Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-17 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 03:59:39 +00:00
|
|
|
func TestMsgf(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
2017-11-05 13:22:20 +00:00
|
|
|
log := New(out)
|
|
|
|
log.Log().Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"message":"one two 3.4 5 six"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-21 03:59:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:21:18 +00:00
|
|
|
func TestWithAndFieldsCombined(t *testing.T) {
|
2017-05-12 05:24:39 +00:00
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).With().Str("f1", "val").Str("f2", "val").Logger()
|
|
|
|
log.Log().Str("f3", "val").Msg("")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"f1":"val","f2":"val","f3":"val"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLevel(t *testing.T) {
|
|
|
|
t.Run("Disabled", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(Disabled)
|
|
|
|
log.Info().Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-12-01 17:52:37 +00:00
|
|
|
t.Run("NoLevel/Disabled", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(Disabled)
|
|
|
|
log.Log().Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
2017-12-01 17:52:37 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoLevel/Info", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(InfoLevel)
|
|
|
|
log.Log().Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"message":"test"}`+"\n"; got != want {
|
2017-12-01 17:52:37 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoLevel/Panic", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(PanicLevel)
|
|
|
|
log.Log().Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"message":"test"}`+"\n"; got != want {
|
2017-12-01 17:52:37 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoLevel/WithLevel", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(InfoLevel)
|
|
|
|
log.WithLevel(NoLevel).Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"message":"test"}`+"\n"; got != want {
|
2017-12-01 17:52:37 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
t.Run("Info", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(InfoLevel)
|
|
|
|
log.Info().Msg("test")
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"info","message":"test"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:44:55 +00:00
|
|
|
func TestGetLevel(t *testing.T) {
|
|
|
|
levels := []Level{
|
|
|
|
DebugLevel,
|
|
|
|
InfoLevel,
|
|
|
|
WarnLevel,
|
|
|
|
ErrorLevel,
|
|
|
|
FatalLevel,
|
|
|
|
PanicLevel,
|
|
|
|
NoLevel,
|
|
|
|
Disabled,
|
|
|
|
}
|
|
|
|
for _, level := range levels {
|
|
|
|
if got, want := New(nil).Level(level).GetLevel(), level; got != want {
|
|
|
|
t.Errorf("GetLevel() = %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
func TestSampling(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
2017-08-29 01:52:15 +00:00
|
|
|
log := New(out).Sample(&BasicSampler{N: 2})
|
2017-05-12 05:24:39 +00:00
|
|
|
log.Log().Int("i", 1).Msg("")
|
|
|
|
log.Log().Int("i", 2).Msg("")
|
|
|
|
log.Log().Int("i", 3).Msg("")
|
|
|
|
log.Log().Int("i", 4).Msg("")
|
2018-09-16 22:53:13 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), "{\"i\":1}\n{\"i\":3}\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:52:52 +00:00
|
|
|
func TestDiscard(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
log.Log().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double call
|
|
|
|
log.Log().Discard().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
type levelWriter struct {
|
|
|
|
ops []struct {
|
|
|
|
l Level
|
|
|
|
p string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *levelWriter) Write(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *levelWriter) WriteLevel(lvl Level, p []byte) (int, error) {
|
2018-03-28 18:49:41 +00:00
|
|
|
p = decodeIfBinaryToBytes(p)
|
2017-05-12 05:24:39 +00:00
|
|
|
lw.ops = append(lw.ops, struct {
|
|
|
|
l Level
|
|
|
|
p string
|
|
|
|
}{lvl, string(p)})
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLevelWriter(t *testing.T) {
|
|
|
|
lw := &levelWriter{
|
|
|
|
ops: []struct {
|
|
|
|
l Level
|
|
|
|
p string
|
|
|
|
}{},
|
|
|
|
}
|
2021-08-27 20:34:08 +00:00
|
|
|
|
|
|
|
// Allow extra-verbose logs.
|
|
|
|
SetGlobalLevel(TraceLevel - 1)
|
|
|
|
log := New(lw).Level(TraceLevel - 1)
|
|
|
|
|
2019-11-04 19:39:22 +00:00
|
|
|
log.Trace().Msg("0")
|
2017-05-12 05:24:39 +00:00
|
|
|
log.Debug().Msg("1")
|
|
|
|
log.Info().Msg("2")
|
|
|
|
log.Warn().Msg("3")
|
|
|
|
log.Error().Msg("4")
|
2017-12-01 17:52:37 +00:00
|
|
|
log.Log().Msg("nolevel-1")
|
2019-11-04 19:39:22 +00:00
|
|
|
log.WithLevel(TraceLevel).Msg("5")
|
|
|
|
log.WithLevel(DebugLevel).Msg("6")
|
|
|
|
log.WithLevel(InfoLevel).Msg("7")
|
|
|
|
log.WithLevel(WarnLevel).Msg("8")
|
|
|
|
log.WithLevel(ErrorLevel).Msg("9")
|
2017-12-01 17:52:37 +00:00
|
|
|
log.WithLevel(NoLevel).Msg("nolevel-2")
|
2021-08-27 20:34:08 +00:00
|
|
|
log.WithLevel(-1).Msg("-1") // Same as TraceLevel
|
|
|
|
log.WithLevel(-2).Msg("-2") // Will log
|
|
|
|
log.WithLevel(-3).Msg("-3") // Will not log
|
2017-07-10 09:56:44 +00:00
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
want := []struct {
|
|
|
|
l Level
|
|
|
|
p string
|
|
|
|
}{
|
2019-11-04 19:39:22 +00:00
|
|
|
{TraceLevel, `{"level":"trace","message":"0"}` + "\n"},
|
2017-05-12 05:24:39 +00:00
|
|
|
{DebugLevel, `{"level":"debug","message":"1"}` + "\n"},
|
|
|
|
{InfoLevel, `{"level":"info","message":"2"}` + "\n"},
|
2017-06-08 17:03:03 +00:00
|
|
|
{WarnLevel, `{"level":"warn","message":"3"}` + "\n"},
|
2017-05-12 05:24:39 +00:00
|
|
|
{ErrorLevel, `{"level":"error","message":"4"}` + "\n"},
|
2017-12-01 17:52:37 +00:00
|
|
|
{NoLevel, `{"message":"nolevel-1"}` + "\n"},
|
2019-11-04 19:39:22 +00:00
|
|
|
{TraceLevel, `{"level":"trace","message":"5"}` + "\n"},
|
|
|
|
{DebugLevel, `{"level":"debug","message":"6"}` + "\n"},
|
|
|
|
{InfoLevel, `{"level":"info","message":"7"}` + "\n"},
|
|
|
|
{WarnLevel, `{"level":"warn","message":"8"}` + "\n"},
|
|
|
|
{ErrorLevel, `{"level":"error","message":"9"}` + "\n"},
|
2017-12-01 17:52:37 +00:00
|
|
|
{NoLevel, `{"message":"nolevel-2"}` + "\n"},
|
2021-08-27 20:34:08 +00:00
|
|
|
{Level(-1), `{"level":"trace","message":"-1"}` + "\n"},
|
|
|
|
{Level(-2), `{"level":"-2","message":"-2"}` + "\n"},
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
if got := lw.ops; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 04:57:46 +00:00
|
|
|
|
|
|
|
func TestContextTimestamp(t *testing.T) {
|
2017-05-20 09:22:57 +00:00
|
|
|
TimestampFunc = func() time.Time {
|
2017-05-20 04:57:46 +00:00
|
|
|
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
|
|
|
}
|
|
|
|
defer func() {
|
2017-05-20 09:22:57 +00:00
|
|
|
TimestampFunc = time.Now
|
2017-05-20 04:57:46 +00:00
|
|
|
}()
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).With().Timestamp().Str("foo", "bar").Logger()
|
|
|
|
log.Log().Msg("hello world")
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","time":"2001-02-03T04:05:06Z","message":"hello world"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-20 04:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventTimestamp(t *testing.T) {
|
2017-05-20 09:22:57 +00:00
|
|
|
TimestampFunc = func() time.Time {
|
2017-05-20 04:57:46 +00:00
|
|
|
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
|
|
|
}
|
|
|
|
defer func() {
|
2017-05-20 09:22:57 +00:00
|
|
|
TimestampFunc = time.Now
|
2017-05-20 04:57:46 +00:00
|
|
|
}()
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).With().Str("foo", "bar").Logger()
|
|
|
|
log.Log().Timestamp().Msg("hello world")
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","time":"2001-02-03T04:05:06Z","message":"hello world"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
2017-05-20 04:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 18:47:56 +00:00
|
|
|
|
|
|
|
func TestOutputWithoutTimestamp(t *testing.T) {
|
|
|
|
ignoredOut := &bytes.Buffer{}
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(ignoredOut).Output(out).With().Str("foo", "bar").Logger()
|
|
|
|
log.Log().Msg("hello world")
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","message":"hello world"}`+"\n"; got != want {
|
2017-11-08 18:47:56 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutputWithTimestamp(t *testing.T) {
|
|
|
|
TimestampFunc = func() time.Time {
|
|
|
|
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
TimestampFunc = time.Now
|
|
|
|
}()
|
|
|
|
ignoredOut := &bytes.Buffer{}
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(ignoredOut).Output(out).With().Timestamp().Str("foo", "bar").Logger()
|
|
|
|
log.Log().Msg("hello world")
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","time":"2001-02-03T04:05:06Z","message":"hello world"}`+"\n"; got != want {
|
2017-11-08 18:47:56 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
|
|
|
|
type loggableError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l loggableError) MarshalZerologObject(e *Event) {
|
2018-09-16 22:53:13 +00:00
|
|
|
e.Str("message", l.error.Error()+": loggableError")
|
2018-07-02 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorMarshalFunc(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
|
|
|
|
// test default behaviour
|
|
|
|
log.Log().Err(errors.New("err")).Msg("msg")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err","message":"msg"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
log.Log().Err(loggableError{errors.New("err")}).Msg("msg")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":{"message":"err: loggableError"},"message":"msg"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
// test overriding the ErrorMarshalFunc
|
|
|
|
originalErrorMarshalFunc := ErrorMarshalFunc
|
2018-09-16 22:53:13 +00:00
|
|
|
defer func() {
|
2018-07-02 19:46:01 +00:00
|
|
|
ErrorMarshalFunc = originalErrorMarshalFunc
|
|
|
|
}()
|
|
|
|
|
|
|
|
ErrorMarshalFunc = func(err error) interface{} {
|
|
|
|
return err.Error() + ": marshaled string"
|
|
|
|
}
|
|
|
|
log.Log().Err(errors.New("err")).Msg("msg")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err: marshaled string","message":"msg"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Reset()
|
|
|
|
ErrorMarshalFunc = func(err error) interface{} {
|
|
|
|
return errors.New(err.Error() + ": new error")
|
|
|
|
}
|
|
|
|
log.Log().Err(errors.New("err")).Msg("msg")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err: new error","message":"msg"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Reset()
|
|
|
|
ErrorMarshalFunc = func(err error) interface{} {
|
|
|
|
return loggableError{err}
|
|
|
|
}
|
|
|
|
log.Log().Err(errors.New("err")).Msg("msg")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":{"message":"err: loggableError"},"message":"msg"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 18:54:42 +00:00
|
|
|
|
2019-02-20 19:39:29 +00:00
|
|
|
func TestCallerMarshalFunc(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
|
|
|
|
// test default behaviour this is really brittle due to the line numbers
|
|
|
|
// actually mattering for validation
|
2022-07-29 14:29:02 +00:00
|
|
|
pc, file, line, _ := runtime.Caller(0)
|
2019-03-02 00:08:23 +00:00
|
|
|
caller := fmt.Sprintf("%s:%d", file, line+2)
|
2019-02-20 19:39:29 +00:00
|
|
|
log.Log().Caller().Msg("msg")
|
2019-03-02 00:08:23 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
2019-02-20 19:39:29 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
// test custom behavior. In this case we'll take just the last directory
|
|
|
|
origCallerMarshalFunc := CallerMarshalFunc
|
|
|
|
defer func() { CallerMarshalFunc = origCallerMarshalFunc }()
|
2022-07-29 14:29:02 +00:00
|
|
|
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
2019-02-20 19:39:29 +00:00
|
|
|
parts := strings.Split(file, "/")
|
|
|
|
if len(parts) > 1 {
|
2019-03-02 00:08:23 +00:00
|
|
|
return strings.Join(parts[len(parts)-2:], "/") + ":" + strconv.Itoa(line)
|
2019-02-20 19:39:29 +00:00
|
|
|
}
|
2019-10-04 14:31:06 +00:00
|
|
|
|
2022-07-29 14:29:02 +00:00
|
|
|
return runtime.FuncForPC(pc).Name() + ":" + file + ":" + strconv.Itoa(line)
|
2019-02-20 19:39:29 +00:00
|
|
|
}
|
2022-07-29 14:29:02 +00:00
|
|
|
pc, file, line, _ = runtime.Caller(0)
|
|
|
|
caller = CallerMarshalFunc(pc, file, line+2)
|
2019-02-20 19:39:29 +00:00
|
|
|
log.Log().Caller().Msg("msg")
|
2019-03-02 00:08:23 +00:00
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
2019-02-20 19:39:29 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 00:08:23 +00:00
|
|
|
func TestLevelFieldMarshalFunc(t *testing.T) {
|
|
|
|
origLevelFieldMarshalFunc := LevelFieldMarshalFunc
|
|
|
|
LevelFieldMarshalFunc = func(l Level) string {
|
|
|
|
return strings.ToUpper(l.String())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
LevelFieldMarshalFunc = origLevelFieldMarshalFunc
|
|
|
|
}()
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out)
|
|
|
|
|
|
|
|
log.Debug().Msg("test")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"DEBUG","message":"test"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
log.Info().Msg("test")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"INFO","message":"test"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
log.Warn().Msg("test")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"WARN","message":"test"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
|
|
|
|
log.Error().Msg("test")
|
|
|
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"ERROR","message":"test"}`+"\n"; got != want {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
|
2018-11-20 18:54:42 +00:00
|
|
|
type errWriter struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w errWriter) Write(p []byte) (n int, err error) {
|
|
|
|
return 0, w.error
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorHandler(t *testing.T) {
|
|
|
|
var got error
|
|
|
|
want := errors.New("write error")
|
|
|
|
ErrorHandler = func(err error) {
|
|
|
|
got = err
|
|
|
|
}
|
|
|
|
log := New(errWriter{want})
|
|
|
|
log.Log().Msg("test")
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("ErrorHandler err = %#v, want %#v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 23:11:26 +00:00
|
|
|
|
|
|
|
func TestUpdateEmptyContext(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
log := New(&buf)
|
|
|
|
|
|
|
|
log.UpdateContext(func(c Context) Context {
|
|
|
|
return c.Str("foo", "bar")
|
|
|
|
})
|
|
|
|
log.Info().Msg("no panic")
|
|
|
|
|
|
|
|
want := `{"level":"info","foo":"bar","message":"no panic"}` + "\n"
|
|
|
|
|
2020-06-25 21:48:07 +00:00
|
|
|
if got := decodeIfBinaryToString(buf.Bytes()); got != want {
|
2020-06-24 23:11:26 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %q\nwant: %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 05:32:06 +00:00
|
|
|
|
|
|
|
func TestLevel_String(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
l Level
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"trace", TraceLevel, "trace"},
|
|
|
|
{"debug", DebugLevel, "debug"},
|
|
|
|
{"info", InfoLevel, "info"},
|
|
|
|
{"warn", WarnLevel, "warn"},
|
|
|
|
{"error", ErrorLevel, "error"},
|
|
|
|
{"fatal", FatalLevel, "fatal"},
|
|
|
|
{"panic", PanicLevel, "panic"},
|
|
|
|
{"disabled", Disabled, "disabled"},
|
|
|
|
{"nolevel", NoLevel, ""},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := tt.l.String(); got != tt.want {
|
|
|
|
t.Errorf("String() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:51:01 +00:00
|
|
|
func TestLevel_MarshalText(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
l Level
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"trace", TraceLevel, "trace"},
|
|
|
|
{"debug", DebugLevel, "debug"},
|
|
|
|
{"info", InfoLevel, "info"},
|
|
|
|
{"warn", WarnLevel, "warn"},
|
|
|
|
{"error", ErrorLevel, "error"},
|
|
|
|
{"fatal", FatalLevel, "fatal"},
|
|
|
|
{"panic", PanicLevel, "panic"},
|
|
|
|
{"disabled", Disabled, "disabled"},
|
|
|
|
{"nolevel", NoLevel, ""},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got, err := tt.l.MarshalText(); err != nil {
|
|
|
|
t.Errorf("MarshalText couldn't marshal: %v", tt.l)
|
|
|
|
} else if string(got) != tt.want {
|
|
|
|
t.Errorf("String() = %v, want %v", string(got), tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 05:32:06 +00:00
|
|
|
func TestParseLevel(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
levelStr string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Level
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"trace", args{"trace"}, TraceLevel, false},
|
|
|
|
{"debug", args{"debug"}, DebugLevel, false},
|
|
|
|
{"info", args{"info"}, InfoLevel, false},
|
|
|
|
{"warn", args{"warn"}, WarnLevel, false},
|
|
|
|
{"error", args{"error"}, ErrorLevel, false},
|
|
|
|
{"fatal", args{"fatal"}, FatalLevel, false},
|
|
|
|
{"panic", args{"panic"}, PanicLevel, false},
|
|
|
|
{"disabled", args{"disabled"}, Disabled, false},
|
|
|
|
{"nolevel", args{""}, NoLevel, false},
|
2021-08-27 20:34:08 +00:00
|
|
|
{"-1", args{"-1"}, TraceLevel, false},
|
|
|
|
{"-2", args{"-2"}, Level(-2), false},
|
|
|
|
{"-3", args{"-3"}, Level(-3), false},
|
2021-04-13 05:32:06 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := ParseLevel(tt.args.levelStr)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ParseLevel() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("ParseLevel() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 19:51:01 +00:00
|
|
|
|
|
|
|
func TestUnmarshalTextLevel(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
levelStr string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Level
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"trace", args{"trace"}, TraceLevel, false},
|
|
|
|
{"debug", args{"debug"}, DebugLevel, false},
|
|
|
|
{"info", args{"info"}, InfoLevel, false},
|
|
|
|
{"warn", args{"warn"}, WarnLevel, false},
|
|
|
|
{"error", args{"error"}, ErrorLevel, false},
|
|
|
|
{"fatal", args{"fatal"}, FatalLevel, false},
|
|
|
|
{"panic", args{"panic"}, PanicLevel, false},
|
|
|
|
{"disabled", args{"disabled"}, Disabled, false},
|
|
|
|
{"nolevel", args{""}, NoLevel, false},
|
|
|
|
{"-1", args{"-1"}, TraceLevel, false},
|
|
|
|
{"-2", args{"-2"}, Level(-2), false},
|
|
|
|
{"-3", args{"-3"}, Level(-3), false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var l Level
|
|
|
|
err := l.UnmarshalText([]byte(tt.args.levelStr))
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if l != tt.want {
|
|
|
|
t.Errorf("UnmarshalText() got = %v, want %v", l, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|