forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_test.go
101 lines (84 loc) · 2.51 KB
/
logging_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
package goa_test
import (
"bytes"
"log"
"github.com/goadesign/goa"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
)
type LogEntry struct {
ctx context.Context
msg string
data []goa.KV
}
type TestLog struct {
infoEntries []*LogEntry
errorEntries []*LogEntry
}
func (l *TestLog) Info(ctx context.Context, msg string, data ...goa.KV) {
l.infoEntries = append(l.infoEntries, &LogEntry{ctx, msg, data})
}
func (l *TestLog) Error(ctx context.Context, msg string, data ...goa.KV) {
l.errorEntries = append(l.errorEntries, &LogEntry{ctx, msg, data})
}
var _ = Describe("Info and Error", func() {
Context("with a nil Log", func() {
BeforeEach(func() {
goa.Log = nil
})
It("Info doesn't log and doesn't crash", func() {
Ω(func() { goa.Info(nil, "foo") }).ShouldNot(Panic())
})
It("Error doesn't log and doesn't crash", func() {
Ω(func() { goa.Error(nil, "foo") }).ShouldNot(Panic())
})
})
Context("with a valid Log", func() {
var testLog *TestLog
var ctx context.Context
ctxData := []goa.KV{{"ctxData", true}, {"other", 42}}
data := []goa.KV{{"data", "foo"}}
const msg = "message"
BeforeEach(func() {
testLog = new(TestLog)
goa.Log = testLog
ctx = goa.NewLogContext(nil, ctxData...)
})
It("Info collects the context data", func() {
goa.Info(ctx, msg, data...)
Ω(testLog.infoEntries).Should(HaveLen(1))
Ω(testLog.infoEntries[0].ctx).Should(Equal(ctx))
Ω(testLog.infoEntries[0].msg).Should(Equal(msg))
Ω(testLog.infoEntries[0].data).Should(Equal(append(ctxData, data...)))
})
It("Error collects the context data", func() {
goa.Error(ctx, msg, data...)
Ω(testLog.errorEntries).Should(HaveLen(1))
Ω(testLog.errorEntries[0].ctx).Should(Equal(ctx))
Ω(testLog.errorEntries[0].msg).Should(Equal(msg))
Ω(testLog.errorEntries[0].data).Should(Equal(append(ctxData, data...)))
})
})
})
var _ = Describe("DefaultLogger", func() {
var logger *goa.DefaultLogger
Context("logging to a buffer", func() {
var buffer *bytes.Buffer
const key = "key"
const value = "value"
const msg = "msg"
ctx := goa.NewLogContext(nil, goa.KV{key, value})
BeforeEach(func() {
buffer = new(bytes.Buffer)
logger = &goa.DefaultLogger{Logger: log.New(buffer, "", log.LstdFlags)}
goa.Log = logger
goa.Info(ctx, msg)
})
It("logs all the context", func() {
Ω(buffer.String()).Should(ContainSubstring(key))
Ω(buffer.String()).Should(ContainSubstring(value))
Ω(buffer.String()).Should(ContainSubstring(msg))
})
})
})