-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxhandler_test.go
122 lines (98 loc) · 4.35 KB
/
ctxhandler_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package slogx_test
import (
"bytes"
"context"
"log/slog"
"os"
"testing"
"github.com/powerman/check"
"github.com/powerman/slogx"
)
func TestEnabled(tt *testing.T) {
t := check.T(tt)
t.Parallel()
h := slog.NewTextHandler(os.Stdout, nil)
slogx.SetDefaultCtxHandler(context.Background(), h)
t.True(slog.Default().Enabled(context.Background(), slog.LevelWarn))
t.False(slog.Default().Enabled(context.Background(), slog.LevelDebug))
h = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelError})
ctx := slogx.NewContextWithHandler(context.Background(), h)
t.True(slog.Default().Enabled(ctx, slog.LevelError))
t.False(slog.Default().Enabled(ctx, slog.LevelWarn))
}
func TestCtxHandler(tt *testing.T) {
t := check.T(tt)
t.Parallel()
var buf bytes.Buffer
var h slog.Handler
// With TextHandler
slogx.SetDefaultCtxHandler(context.Background(), slog.NewTextHandler(&buf, nil))
h = slog.NewTextHandler(&buf, nil).WithGroup("g").WithAttrs([]slog.Attr{slog.String("key1", "value1"), slog.String("key2", "value2")})
ctx := slogx.NewContextWithHandler(context.Background(), h)
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `level=INFO msg="some message" g.key1=value1 g.key2=value2`)
buf.Reset()
log := slog.With(slog.String("key1", "modified"))
log.InfoContext(ctx, "some message")
t.Match(buf.String(), `level=INFO msg="some message" g.key1=value1 g.key2=value2 g.key1=modified`)
buf.Reset()
log = log.WithGroup("g2").With(slog.String("key3", "value3"))
log.InfoContext(ctx, "some message")
t.Match(buf.String(), `level=INFO msg="some message" g.key1=value1 g.key2=value2 g.key1=modified g.g2.key3=value3`)
buf.Reset()
slog.InfoContext(ctx, "some message", "key4", "value4")
t.Match(buf.String(), `level=INFO msg="some message" g.key1=value1 g.key2=value2 g.key4=value4`)
// With JsonHandler
slogx.SetDefaultCtxHandler(context.Background(), slog.NewJSONHandler(&buf, nil))
h = slog.NewJSONHandler(&buf, nil).WithGroup("g").WithAttrs([]slog.Attr{slog.String("key1", "value1"), slog.String("key2", "value2")})
ctx = slogx.NewContextWithHandler(context.Background(), h)
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `"level":"INFO","msg":"some message","g":{"key1":"value1","key2":"value2"}}`)
buf.Reset()
log = slog.With(slog.String("key1", "modified"))
log.InfoContext(ctx, "some message")
t.Match(buf.String(), `"level":"INFO","msg":"some message","g":{"key1":"value1","key2":"value2","key1":"modified"}}`)
buf.Reset()
log = log.WithGroup("g2").With(slog.String("key3", "value3"))
log.InfoContext(ctx, "some message")
t.Match(buf.String(), `"level":"INFO","msg":"some message","g":{"key1":"value1","key2":"value2","key1":"modified","g2":{"key3":"value3"}}}`)
buf.Reset()
slog.InfoContext(ctx, "some message", "key4", "value4")
t.Match(buf.String(), `"level":"INFO","msg":"some message","g":{"key1":"value1","key2":"value2","key4":"value4"}}`)
// WithAttrs/WithGroup with empty parameter
handler := slog.Default().Handler()
t.DeepEqual(handler.WithAttrs([]slog.Attr{}), handler)
t.DeepEqual(handler.WithGroup(""), handler)
}
func TestContextWith(tt *testing.T) {
t := check.T(tt)
t.Parallel()
var buf bytes.Buffer
ctx := slogx.SetDefaultCtxHandler(context.Background(), slog.NewTextHandler(&buf, nil))
ctx = slogx.ContextWithAttrs(ctx, "key1", "value1")
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `"some message" key1=value1`)
buf.Reset()
ctx = slogx.ContextWithGroup(ctx, "g1")
ctx = slogx.ContextWithAttrs(ctx, "key2", "value2")
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `"some message" key1=value1 g1.key2=value2`)
buf.Reset()
ctx = slogx.ContextWithGroup(ctx, "g2")
ctx = slogx.ContextWithAttrs(ctx, "key3", 3)
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `"some message" key1=value1 g1.key2=value2 g1.g2.key3=3`)
}
func TestLaxCtxHandler(tt *testing.T) {
t := check.T(tt)
var buf bytes.Buffer
ctx := context.Background()
h := slog.NewTextHandler(&buf, nil).WithAttrs([]slog.Attr{slog.String("key1", "value1")})
slogx.SetDefaultCtxHandler(context.Background(), h)
slog.InfoContext(ctx, "some message")
t.Match(buf.String(), `level=INFO msg="some message" key1=value1 !BADCTX=context.Background`)
buf.Reset()
slogx.SetDefaultCtxHandler(context.Background(), h, slogx.LaxCtxHandler())
slog.InfoContext(ctx, "some message")
t.NotMatch(buf.String(), "!BADCTX")
}