forked from nownabe/clog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels_test.go
79 lines (56 loc) · 2.04 KB
/
labels_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
package clog_test
import (
"context"
"testing"
"go.nownabe.dev/clog"
)
func Test_ContextWithLabels(t *testing.T) {
t.Parallel()
l, w := newLogger(clog.SeverityInfo)
ctx := context.Background()
l.Info(ctx, "msg1")
w.assertLog(t, buildWantLog("INFO", "msg1"))
func(ctx context.Context) {
ctx, removeLabel := clog.ContextWithLabel(ctx, "lk1", "lv1")
defer removeLabel()
l.Info(ctx, "msg2")
w.assertLog(t, buildWantLog("INFO", "msg2", keyLabels, map[string]any{"lk1": "lv1"}))
func(ctx context.Context) {
ctx, removeLabel := clog.ContextWithLabel(ctx, "lk2", "lv2")
defer removeLabel()
l.Info(ctx, "msg3")
w.assertLog(t, buildWantLog("INFO", "msg3", keyLabels, map[string]any{"lk1": "lv1", "lk2": "lv2"}))
}(ctx)
l.Info(ctx, "msg4")
w.assertLog(t, buildWantLog("INFO", "msg4", keyLabels, map[string]any{"lk1": "lv1"}))
}(ctx)
l.Info(ctx, "msg5")
w.assertLog(t, buildWantLog("INFO", "msg5"))
}
func Test_DefaultLables(t *testing.T) {
t.Parallel()
defaultLabels := map[string]string{
"lk1": "lv1",
"lk2": "lv2",
}
l, w := newLogger(clog.SeverityInfo, clog.WithLabels(defaultLabels))
ctx := context.Background()
l.Info(ctx, "msg1")
w.assertLog(t, buildWantLog("INFO", "msg1", keyLabels, map[string]any{"lk1": "lv1", "lk2": "lv2"}))
func(ctx context.Context) {
ctx, removeLabel := clog.ContextWithLabel(ctx, "lk3", "lv3")
defer removeLabel()
l.Info(ctx, "msg2")
w.assertLog(t, buildWantLog("INFO", "msg2", keyLabels, map[string]any{"lk1": "lv1", "lk2": "lv2", "lk3": "lv3"}))
func(ctx context.Context) {
ctx, removeLabel := clog.ContextWithLabel(ctx, "lk2", "LV2")
defer removeLabel()
l.Info(ctx, "msg3")
w.assertLog(t, buildWantLog("INFO", "msg3", keyLabels, map[string]any{"lk1": "lv1", "lk2": "LV2", "lk3": "lv3"}))
}(ctx)
l.Info(ctx, "msg4")
w.assertLog(t, buildWantLog("INFO", "msg4", keyLabels, map[string]any{"lk1": "lv1", "lk2": "lv2", "lk3": "lv3"}))
}(ctx)
l.Info(ctx, "msg5")
w.assertLog(t, buildWantLog("INFO", "msg5", keyLabels, map[string]any{"lk1": "lv1", "lk2": "lv2"}))
}