-
Notifications
You must be signed in to change notification settings - Fork 10
/
slog_handler_test.go
142 lines (126 loc) · 3.09 KB
/
slog_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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package coralogix
import (
"errors"
"fmt"
"log/slog"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSlogHandler_WithAttrs(t *testing.T) {
onTest1 := &CoralogixHandler{}
assert.Empty(t, onTest1.data)
onTest2 := onTest1.WithAttrs([]slog.Attr{
slog.String("1", "1"),
slog.Int("2", 2),
})
assert.Equal(t, map[string]interface{}{
"1": "1",
"2": int64(2),
}, onTest2.(*CoralogixHandler).data)
}
func TestSlogHandler_AttrToMap(t *testing.T) {
m1 := map[string]any{}
attrToMap(m1, slog.String("1", "1"))
attrToMap(m1, slog.Int("2", 2))
attrToMap(m1, slog.Any("error", errors.New("test")))
assert.Equal(t, map[string]any{
"1": "1",
"2": int64(2),
"error": "test",
}, m1)
m2 := map[string]any{}
attrToMap(m2, slog.String("1", "1"))
attrToMap(m2, slog.Any("arr", []string{"1", "2"}))
assert.Equal(t, map[string]any{
"1": "1",
"arr": []string{"1", "2"},
}, m2)
m3 := map[string]any{}
attrToMap(m3, slog.String("1", "1"))
attrToMap(m3, slog.Group("group",
slog.String("2", "2"),
slog.Any("arr", []string{"1", "2"}),
))
assert.Equal(t, map[string]any{
"1": "1",
"group": map[string]any{
"2": "2",
"arr": []string{"1", "2"},
},
}, m3)
}
func TestSlogHandler_Send(t *testing.T) {
coralogixHandler := NewCoralogixHandler(
GetEnv(
"PRIVATE_KEY",
testPrivateKey,
),
"sdk-go",
"test",
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
)
defer func() { recover() }()
defer coralogixHandler.Stop()
log := slog.New(coralogixHandler)
slog.SetDefault(log)
attr := slog.Attr{Key: "extra", Value: slog.StringValue("additional")}
testcases := []struct {
name string
logfn func(message string, args ...interface{})
severity uint
}{
{
name: "test debug",
severity: Level.DEBUG,
logfn: func(message string, args ...interface{}) {
log.Debug(message, args...)
},
},
{
name: "test info",
severity: Level.INFO,
logfn: func(message string, args ...interface{}) {
log.Info(message, args...)
},
},
{
name: "test warn",
severity: Level.WARNING,
logfn: func(message string, args ...interface{}) {
log.Warn(message, args...)
},
},
{
name: "test error",
severity: Level.ERROR,
logfn: func(message string, args ...interface{}) {
log.Error(message, args...)
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
msg := fmt.Sprintf("%s (%s)", tc.name, t.Name())
tc.logfn(msg, attr)
var bulk *Bulk
assert.Eventually(t, func() (ok bool) {
bulk, ok = mockHTTPServerMap[t.Name()]
return ok
}, 5*time.Second, 1*time.Second, "%s key not found in mockHTTPServerMap", t.Name())
var msgExists bool
for _, entry := range bulk.LogEntries {
if msgExists = strings.Contains(entry.Text, tc.name); msgExists {
assert.Equal(t, tc.severity, entry.Severity)
assert.True(t, strings.Contains(entry.Text, attr.Value.String()),
"entry Text does not contain extra field", entry.Text, attr)
break
}
}
assert.True(t, msgExists, "no matching message found", string(bulk.ToJSON()))
})
}
}