-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
90 lines (69 loc) · 1.81 KB
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package slogmw_test
import (
"bytes"
"context"
"log/slog"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zknill/slogmw"
)
func TestFormat(t *testing.T) {
t.Parallel()
now, _ := time.Parse(time.DateTime, "2023-08-09 12:12:04")
opts := &slog.HandlerOptions{
ReplaceAttr: slogmw.FormatChain(
staticTime(now),
slogmw.FormatTime(slog.TimeKey, time.DateTime),
slogmw.FormatField(slog.MessageKey, func(a slog.Attr) slog.Attr {
a.Value = slog.StringValue(a.Value.String() + " edited")
return a
}),
slogmw.FormatKey(slog.MessageKey, "log"),
),
}
b := bytes.Buffer{}
logger := slog.New(slog.NewTextHandler(&b, opts))
logger.Info("hello world")
want := "time=\"2023-08-09 12:12:04\" level=INFO log=\"hello world edited\"\n"
assert.Equal(t, want, b.String())
}
func staticTime(t time.Time) slogmw.AttrFormatFunc {
return func(a slog.Attr) slog.Attr {
if a.Key != slog.TimeKey {
return a
}
a.Value = slog.TimeValue(t)
return a
}
}
func TestWrapHandler(t *testing.T) {
t.Parallel()
ctx := context.WithValue(context.Background(), "key", "my-value")
now, _ := time.Parse(time.DateTime, "2023-08-09 12:12:04")
opts := &slog.HandlerOptions{
ReplaceAttr: slogmw.FormatChain(
staticTime(now),
slogmw.FormatTime(slog.TimeKey, time.DateTime),
),
}
b := bytes.Buffer{}
h := slogmw.WrapHandler(
slog.NewJSONHandler(&b, opts),
slogmw.IncludeStatic(slog.String("a", "b")),
slogmw.IncludeContext(func(ctx context.Context) []slog.Attr {
v := ctx.Value("key").(string)
return []slog.Attr{slog.String("my-key", v)}
}),
)
logger := slog.New(h)
logger.InfoContext(ctx, "hello world")
want := `{
"time":"2023-08-09 12:12:04",
"a": "b",
"level": "INFO",
"msg": "hello world",
"my-key": "my-value"
}`
assert.JSONEq(t, want, b.String())
}