forked from nownabe/clog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
52 lines (40 loc) · 1.22 KB
/
handler_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
package clog_test
import (
"context"
"log/slog"
"testing"
"go.nownabe.dev/clog"
)
func Test_WithHandleFunc(t *testing.T) {
t.Parallel()
type ctxkey struct{}
f := func(next clog.HandleFunc) clog.HandleFunc {
return (func(ctx context.Context, r slog.Record) error {
if userID, ok := ctx.Value(ctxkey{}).(string); ok {
r.AddAttrs(slog.String("user_id", userID))
}
return next(ctx, r)
})
}
l, w := newLogger(clog.SeverityInfo, clog.WithHandleFunc(f))
ctx := context.WithValue(context.Background(), ctxkey{}, "user1")
l.Info(ctx, "msg1")
w.assertLog(t, buildWantLog("INFO", "msg1", "user_id", "user1"))
}
func TestWithHandleFunc_WithAttrs(t *testing.T) {
t.Parallel()
type ctxkey struct{}
f := func(next clog.HandleFunc) clog.HandleFunc {
return (func(ctx context.Context, r slog.Record) error {
if userID, ok := ctx.Value(ctxkey{}).(string); ok {
r.AddAttrs(slog.String("user_id", userID))
}
return next(ctx, r)
})
}
l, w := newLogger(clog.SeverityInfo, clog.WithHandleFunc(f))
ctx := context.WithValue(context.Background(), ctxkey{}, "user1")
l = l.WithInsertID("insertid")
l.Info(ctx, "msg1")
w.assertLog(t, buildWantLog("INFO", "msg1", "user_id", "user1", keyInsertID, "insertid"))
}