You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

3 years ago
package zap_test
import (
"bytes"
"context"
"testing"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
zlog "gitoa.ru/go-4devs/log/handler/zap"
"gitoa.ru/go-4devs/log/level"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestNew(t *testing.T) {
ctx := context.Background()
buf := &bytes.Buffer{}
core := zapcore.NewCore(zapcore.NewJSONEncoder(zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}), zapcore.AddSync(buf), zapcore.DebugLevel)
logger := zlog.New(zap.New(core))
3 years ago
expect := `{"level":"info","msg":"handle zap message","env":"test"}` + "\n"
if _, err := logger(ctx, entry.New(
entry.WithFields(field.String("env", "test")),
entry.WithLevel(level.Notice),
entry.WithMessage("handle zap message"),
)); err != nil {
t.Error(err)
}
if buf.String() != expect {
t.Errorf("invalid message\n got: %s\nexpect:%s\n", buf.String(), expect)
}
}