-
Notifications
You must be signed in to change notification settings - Fork 28
/
slog_sink_test.go
83 lines (69 loc) · 1.98 KB
/
slog_sink_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
//go:build go1.21
package lager_test
import (
"bytes"
"code.cloudfoundry.org/lager/v3"
"encoding/json"
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"log/slog"
)
var _ = Describe("NewSlogSink", func() {
var (
buf bytes.Buffer
logger lager.Logger
)
matchTimestamp := MatchRegexp(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{5,9}Z$`)
parsedLogMessage := func() (receiver map[string]any) {
Expect(json.Unmarshal(buf.Bytes(), &receiver)).To(Succeed())
return
}
BeforeEach(func() {
buf = bytes.Buffer{}
logger = lager.NewLogger("fake-component")
logger.RegisterSink(lager.NewSlogSink(slog.New(slog.NewJSONHandler(&buf, nil))))
})
It("logs Info()", func() {
logger.Info("fake-info", lager.Data{"foo": "bar"})
Expect(parsedLogMessage()).To(MatchAllKeys(Keys{
"time": matchTimestamp,
"level": Equal("INFO"),
"msg": Equal("fake-component.fake-info"),
"foo": Equal("bar"),
}))
})
It("logs Debug()", func() {
logger.Debug("fake-debug", lager.Data{"foo": "bar"})
Expect(parsedLogMessage()).To(MatchAllKeys(Keys{
"time": matchTimestamp,
"level": Equal("DEBUG"),
"msg": Equal("fake-component.fake-debug"),
"foo": Equal("bar"),
}))
})
It("logs Error()", func() {
logger.Error("fake-error", fmt.Errorf("boom"), lager.Data{"foo": "bar"})
Expect(parsedLogMessage()).To(MatchAllKeys(Keys{
"time": matchTimestamp,
"error": Equal("boom"),
"level": Equal("ERROR"),
"msg": Equal("fake-component.fake-error"),
"foo": Equal("bar"),
}))
})
It("logs Fatal()", func() {
Expect(func() {
logger.Fatal("fake-fatal", fmt.Errorf("boom"), lager.Data{"foo": "bar"})
}).To(Panic())
Expect(parsedLogMessage()).To(MatchAllKeys(Keys{
"time": matchTimestamp,
"error": Equal("boom"),
"level": Equal("ERROR"),
"msg": Equal("fake-component.fake-fatal"),
"foo": Equal("bar"),
"trace": ContainSubstring(`code.cloudfoundry.org/lager/v3.(*logger).Fatal`),
}))
})
})