more test for zerolog fields

pull/2859/head
billy 2 years ago
parent 69158213a3
commit 95c161dc94
  1. 74
      contrib/log/zerolog/zerolog_test.go

@ -3,43 +3,79 @@ package zerolog
import ( import (
"errors" "errors"
"testing" "testing"
"time"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
type testWriter struct { type testWriter struct {
output []string output string
} }
func (x *testWriter) Write(p []byte) (n int, err error) { func (w *testWriter) Write(p []byte) (n int, err error) {
x.output = append(x.output, string(p)) w.output = string(p)
return len(p), nil return len(p), nil
} }
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
out := &testWriter{output: make([]string, 0, 16)} out := &testWriter{}
ll := zerolog.New(out) ll := zerolog.New(out)
logger := NewLogger(ll) logger := NewLogger(ll)
zlog := log.NewHelper(logger) zlog := log.NewHelper(logger)
zlog.Debugw("id", int64(2234222344)) testcases := []struct {
zlog.Infow("obj", map[string]interface{}{"f1": "\x1ffoo\x1fbar\x1fbazemoji \u2764\ufe0f!", "f2": 342342333, "f3": false}) fn func(keyvals ...interface{})
zlog.Warnw("msg", "id not exist", "id", uint64(384383343242)) keyvals []interface{}
zlog.Errorw("err", errors.New("io error")) expect string
zlog.Infow("msg", "test marshal", "except warn") }{
{
except := []string{ fn: zlog.Debugw,
`{"level":"debug","id":2234222344}` + "\n", keyvals: []interface{}{"id", int64(2234222344), "int32", int32(-7463882), "int16", int16(8384), "int8", int8(22), "int", int(-328833)},
`{"level":"info","obj":{"f1":"\u001ffoo\u001fbar\u001fbazemoji ❤!","f2":342342333,"f3":false}}` + "\n", expect: `{"level":"debug","id":2234222344,"int32":-7463882,"int16":8384,"int8":22,"int":-328833}` + "\n",
`{"level":"warn","msg":"id not exist","id":384383343242}` + "\n", },
`{"level":"error","err":"io error"}` + "\n", {
`{"level":"warn","msg":"test marshal","except warn":"KEYVALS UNPAIRED"}` + "\n", fn: zlog.Debugw,
keyvals: []interface{}{"uint8", uint8(35), "uint16", uint16(2134), "uint32", uint32(32345), "uint64", uint64(88739222), "uint", uint(328833)},
expect: `{"level":"debug","uint8":35,"uint16":2134,"uint32":32345,"uint64":88739222,"uint":328833}` + "\n",
},
{
fn: zlog.Debugw,
keyvals: []interface{}{"float32", float32(35.0000001), "float64", float64(213444863.3329988888999)},
expect: `{"level":"debug","float32":35,"float64":213444863.3329989}` + "\n",
},
{
fn: zlog.Infow,
keyvals: []interface{}{"ts", time.Date(2023, 6, 1, 12, 13, 14, 0, time.UTC), "dur", 34 * time.Second},
expect: `{"level":"info","ts":"2023-06-01T12:13:14Z","dur":34000}` + "\n",
},
{
fn: zlog.Infow,
keyvals: []interface{}{"obj", map[string]interface{}{"f1": "\x1ffoo\x1fbar\x1fbazemoji \u2764\ufe0f!", "f2": 342342333, "f3": false}},
expect: `{"level":"info","obj":{"f1":"\u001ffoo\u001fbar\u001fbazemoji ❤!","f2":342342333,"f3":false}}` + "\n",
},
{
fn: zlog.Warnw,
keyvals: []interface{}{"msg", "id not exist", "id", uint64(384383343242)},
expect: `{"level":"warn","msg":"id not exist","id":384383343242}` + "\n",
},
{
fn: zlog.Errorw,
keyvals: []interface{}{"err", errors.New("io error")},
expect: `{"level":"error","err":"io error"}` + "\n",
},
{
fn: zlog.Infow,
keyvals: []interface{}{"msg", "test marshal", "except warn"},
expect: `{"level":"warn","msg":"test marshal","except warn":"KEYVALS UNPAIRED"}` + "\n",
},
} }
for i, s := range except {
if s != out.output[i] { for _, s := range testcases {
t.Fatalf("except=%s, got=%s", s, out.output[i]) s.fn(s.keyvals...)
if s.expect != out.output {
t.Fatalf("except=%s, got=%s", s.expect, out.output)
} }
} }
} }

Loading…
Cancel
Save