1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00
clog/handler_test.go
Billy Lynch 5565dbc976
Add context logger.
This is heavily inspired by https://pkg.go.dev/knative.dev/pkg/logging

Also adds top level logging methods for convinience.
2023-12-12 18:29:22 -05:00

53 lines
941 B
Go

package slogctx
import (
"bytes"
"context"
"encoding/json"
"log/slog"
"reflect"
"testing"
)
func TestContextHandler(t *testing.T) {
ctx := context.Background()
ctx = WithValues(ctx, "foo", "bar")
ctx2 := WithValues(ctx,
"a", "b",
"c", "d",
)
ctx = WithValues(ctx, "b", 1)
for _, tc := range []struct {
ctx context.Context
want map[string]any
}{
{ctx, map[string]any{
"b": float64(1),
"foo": "bar",
}},
{ctx2, map[string]any{
"foo": "bar",
"a": "b",
"c": "d",
}},
} {
t.Run("", func(t *testing.T) {
b := new(bytes.Buffer)
log := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
log.InfoContext(tc.ctx, "")
tc.want["level"] = "INFO"
tc.want["msg"] = ""
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tc.want, got) {
t.Errorf("want %v, got %v", tc.want, got)
}
})
}
}