zen/core/zlog/zlog_test.go

47 lines
876 B
Go
Raw Normal View History

2024-09-03 13:38:45 +07:00
package zlog_test
import (
"context"
"log/slog"
"strings"
"testing"
"time"
"github.com/kinbiko/jsonassert"
"gitlab.bareksa.com/backend/zen/core/zcaller"
"gitlab.bareksa.com/backend/zen/core/zlog"
)
func Test_New(t *testing.T) {
ja := jsonassert.New(t)
output := &strings.Builder{}
z := zlog.New(output, nil)
date := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
record := slog.NewRecord(date, slog.LevelInfo, "test", zcaller.Current())
if err := z.Handle(context.Background(), record); err != nil {
t.Fatal(err)
}
const want = `{
"level": "INFO",
"message": "test",
"time": "<<PRESENCE>>",
"caller": {
"file": "zlog_test.go",
"line": 22,
"function": "zlog_test.Test_New"
}
}`
got := strings.TrimSpace(output.String())
defer func() {
if t.Failed() {
t.Log("got:", got)
t.Log("want:", want)
}
}()
ja.Assertf(got, want)
}