2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-05-17 05:21:18 +00:00
|
|
|
"errors"
|
2017-05-12 05:24:39 +00:00
|
|
|
"reflect"
|
|
|
|
"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("")
|
|
|
|
if got, want := out.String(), "{}\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("")
|
|
|
|
if got, want := out.String(), `{"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("")
|
|
|
|
if got, want := out.String(), `{"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("")
|
|
|
|
if got, want := out.String(), `{"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("")
|
|
|
|
if got, want := out.String(), `{"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("")
|
|
|
|
if got, want := out.String(), `{"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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWith(t *testing.T) {
|
2017-05-17 05:21:18 +00:00
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).With().
|
|
|
|
Str("foo", "bar").
|
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).
|
2017-05-17 05:21:18 +00:00
|
|
|
Float32("float32", 11).
|
|
|
|
Float64("float64", 12).
|
|
|
|
Time("time", time.Time{}).
|
|
|
|
Logger()
|
|
|
|
log.Log().Msg("")
|
|
|
|
if got, want := out.String(), `{"foo":"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,"time":"0001-01-01T00:00:00Z"}`+"\n"; got != want {
|
2017-07-10 09:56:44 +00:00
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
"dur": 1 * time.Second,
|
|
|
|
"time": time.Time{},
|
|
|
|
}).Msg("")
|
|
|
|
if got, want := out.String(), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"nil":null,"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\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()
|
2017-05-17 05:21:18 +00:00
|
|
|
log.Log().
|
2017-07-10 09:56:44 +00:00
|
|
|
Str("string", "foo").
|
|
|
|
Bytes("bytes", []byte("bar")).
|
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).
|
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("")
|
2017-07-10 09:56:44 +00:00
|
|
|
if got, want := out.String(), `{"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,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
|
|
|
|
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").
|
|
|
|
Bytes("bytes", []byte("bar")).
|
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).
|
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("")
|
|
|
|
if got, want := out.String(), ""; 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{}
|
|
|
|
New(out).Log().Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
|
|
|
if got, want := out.String(), `{"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("")
|
|
|
|
if got, want := out.String(), `{"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")
|
|
|
|
if got, want := out.String(), ""; 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("Info", func(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Level(InfoLevel)
|
|
|
|
log.Info().Msg("test")
|
|
|
|
if got, want := out.String(), `{"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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSampling(t *testing.T) {
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := New(out).Sample(2)
|
|
|
|
log.Log().Int("i", 1).Msg("")
|
|
|
|
log.Log().Int("i", 2).Msg("")
|
|
|
|
log.Log().Int("i", 3).Msg("")
|
|
|
|
log.Log().Int("i", 4).Msg("")
|
|
|
|
if got, want := out.String(), "{\"sample\":2,\"i\":2}\n{\"sample\":2,\"i\":4}\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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}{},
|
|
|
|
}
|
|
|
|
log := New(lw)
|
|
|
|
log.Debug().Msg("1")
|
|
|
|
log.Info().Msg("2")
|
|
|
|
log.Warn().Msg("3")
|
|
|
|
log.Error().Msg("4")
|
2017-07-10 09:56:44 +00:00
|
|
|
log.WithLevel(DebugLevel).Msg("5")
|
|
|
|
log.WithLevel(InfoLevel).Msg("6")
|
|
|
|
log.WithLevel(WarnLevel).Msg("7")
|
|
|
|
log.WithLevel(ErrorLevel).Msg("8")
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
want := []struct {
|
|
|
|
l Level
|
|
|
|
p string
|
|
|
|
}{
|
|
|
|
{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-07-10 09:56:44 +00:00
|
|
|
{DebugLevel, `{"level":"debug","message":"5"}` + "\n"},
|
|
|
|
{InfoLevel, `{"level":"info","message":"6"}` + "\n"},
|
|
|
|
{WarnLevel, `{"level":"warn","message":"7"}` + "\n"},
|
|
|
|
{ErrorLevel, `{"level":"error","message":"8"}` + "\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")
|
|
|
|
|
2017-05-20 05:14:51 +00:00
|
|
|
if got, want := out.String(), `{"time":"2001-02-03T04:05:06Z","foo":"bar","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")
|
|
|
|
|
2017-05-20 05:14:51 +00:00
|
|
|
if got, want := out.String(), `{"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
|
|
|
}
|
|
|
|
}
|