-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_test.go
97 lines (88 loc) · 1.87 KB
/
replace_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
package sloggcp_test
import (
"github.com/vlad-tokarev/sloggcp"
"log/slog"
"os"
"testing"
"time"
)
var (
someTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
someSource = slog.Source{
Function: "test",
File: "test.go",
Line: 1,
}
)
// ExampleReplaceAttr shows how to replace default slog attributes with GCP compatible ones
// It writes to stderr, that is why output is empty
func ExampleReplaceAttr() {
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
ReplaceAttr: sloggcp.ReplaceAttr,
AddSource: true,
Level: slog.LevelDebug,
}))
logger.Debug("test",
slog.String("test", "test"),
)
// Output:
}
func TestReplaceAttr(t *testing.T) {
type args struct {
groups []string
a slog.Attr
}
tests := []struct {
name string
args args
want slog.Attr
}{
{
name: "TimeKey",
args: args{
groups: []string{},
a: slog.Time(slog.TimeKey, someTime),
},
want: slog.Time("time", someTime),
},
{
name: "LevelKey",
args: args{
groups: []string{},
a: slog.Any(slog.LevelKey, slog.LevelDebug),
},
want: slog.String("severity", "DEBUG"),
},
{
name: "SourceKey",
args: args{
groups: []string{},
a: slog.Any(slog.SourceKey, &someSource),
},
want: slog.Any("logging.googleapis.com/sourceLocation", &someSource),
},
{
name: "MessageKey",
args: args{
groups: []string{},
a: slog.String(slog.MessageKey, "test"),
},
want: slog.String("message", "test"),
},
{
name: "OtherKey",
args: args{
groups: []string{},
a: slog.String("test", "test"),
},
want: slog.String("test", "test"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sloggcp.ReplaceAttr(tt.args.groups, tt.args.a); !got.Equal(tt.want) {
t.Errorf("ReplaceAttr() = %v, want %v", got, tt.want)
}
})
}
}