-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
142 lines (132 loc) · 3.54 KB
/
logger_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 echo
import (
"bytes"
"io/ioutil"
"os"
"testing"
"bou.ke/monkey"
"github.com/labstack/gommon/log"
. "github.com/smartystreets/goconvey/convey"
)
func TestLogger(t *testing.T) {
Convey("Try logger", t, func(c C) {
var (
b = new(bytes.Buffer)
z = newTestLogger(testBuffer{Buffer: b})
l = NewLogger(z)
exitCode = 0
)
monkey.Patch(os.Exit, func(code int) { exitCode = code })
c.Convey("try nullWriter", func(c C) {
out := l.Output()
size, err := out.Write(make([]byte, 10))
c.So(out, ShouldEqual, ioutil.Discard)
c.So(size, ShouldEqual, 10)
c.So(err, ShouldBeNil)
})
c.Convey("try customize", func(c C) {
l.SetLevel(log.ERROR) // do nothing
l.SetOutput(os.Stdout) // do nothing
l.SetPrefix("prefix") // do nothing
l.SetHeader("header") // do nothing
c.So(l.Prefix(), ShouldEqual, "")
c.So(l.Level(), ShouldEqual, log.DEBUG)
})
c.Convey("try Print", func(c C) {
l.Print("")
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Printf", func(c C) {
l.Printf("")
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Printj", func(c C) {
l.Printj(log.JSON{})
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Debug", func(c C) {
l.Debug("")
c.So(b.String(), ShouldContainSubstring, "debug")
})
c.Convey("try Debugf", func(c C) {
l.Debugf("")
c.So(b.String(), ShouldContainSubstring, "debug")
})
c.Convey("try Debugj", func(c C) {
l.Debugj(log.JSON{})
c.So(b.String(), ShouldContainSubstring, "debug")
})
c.Convey("try Info", func(c C) {
l.Info("")
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Infof", func(c C) {
l.Infof("")
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Infoj", func(c C) {
l.Infoj(log.JSON{})
c.So(b.String(), ShouldContainSubstring, "info")
})
c.Convey("try Warn", func(c C) {
l.Warn("")
c.So(b.String(), ShouldContainSubstring, "warn")
})
c.Convey("try Warnf", func(c C) {
l.Warnf("")
c.So(b.String(), ShouldContainSubstring, "warn")
})
c.Convey("try Warnj", func(c C) {
l.Warnj(log.JSON{})
c.So(b.String(), ShouldContainSubstring, "warn")
})
c.Convey("try Error", func(c C) {
l.Error("")
c.So(b.String(), ShouldContainSubstring, "error")
})
c.Convey("try Errorf", func(c C) {
l.Errorf("")
c.So(b.String(), ShouldContainSubstring, "error")
})
c.Convey("try Errorj", func(c C) {
l.Errorj(log.JSON{})
c.So(b.String(), ShouldContainSubstring, "error")
})
c.Convey("try Fatal", func(c C) {
l.Fatal("")
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "fatal")
})
c.Convey("try Fatalf", func(c C) {
l.Fatalf("")
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "fatal")
})
c.Convey("try Fatalj", func(c C) {
l.Fatalj(log.JSON{})
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "fatal")
})
c.Convey("try Panic", func(c C) {
c.So(func() {
l.Panic("")
}, ShouldPanic)
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "panic")
})
c.Convey("try Panicf", func(c C) {
c.So(func() {
l.Panicf("")
}, ShouldPanic)
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "panic")
})
c.Convey("try Panicj", func(c C) {
c.So(func() {
l.Panicj(log.JSON{})
}, ShouldPanic)
c.So(exitCode, ShouldNotEqual, 2)
c.So(b.String(), ShouldContainSubstring, "panic")
})
})
}