Repository navigation
Expand file tree
/
Copy pathlogger_test.go
More file actions
163 lines (129 loc) · 2.94 KB
/
Copy pathlogger_test.go
File metadata and controls
163 lines (129 loc) · 2.94 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package logging_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/ao-concepts/logging"
"github.com/stretchr/testify/assert"
)
type writer struct {
Logs []string
}
func (w *writer) Write(p []byte) (n int, err error) {
w.Logs = append(w.Logs, string(p))
return len(p), nil
}
func TestLogger_Output(t *testing.T) {
assert := assert.New(t)
// debug
value := "debug"
testMessage(assert, func(log logging.Logger) {
log.ErrDebug(fmt.Errorf(value))
}, value)
testMessage(assert, func(log logging.Logger) {
log.Debug(value)
}, value)
// info
value = "info"
testMessage(assert, func(log logging.Logger) {
log.ErrInfo(fmt.Errorf(value))
}, value)
testMessage(assert, func(log logging.Logger) {
log.Info(value)
}, value)
// warn
value = "warn"
testMessage(assert, func(log logging.Logger) {
log.ErrWarn(fmt.Errorf(value))
}, value)
testMessage(assert, func(log logging.Logger) {
log.Warn(value)
}, value)
// error
value = "error"
testMessage(assert, func(log logging.Logger) {
log.ErrError(fmt.Errorf(value))
}, value)
testMessage(assert, func(log logging.Logger) {
log.Error(value)
}, value)
}
func TestLogger(t *testing.T) {
assert := assert.New(t)
// debug level
w := writer{}
l := logging.New(logging.Debug, &w)
runLogs(l, assert)
assert.Len(w.Logs, 10)
// info level
w = writer{}
l = logging.New(logging.Info, &w)
runLogs(l, assert)
assert.Len(w.Logs, 8)
// warn level
w = writer{}
l = logging.New(logging.Warn, &w)
runLogs(l, assert)
assert.Len(w.Logs, 6)
// error level
w = writer{}
l = logging.New(logging.Error, &w)
runLogs(l, assert)
assert.Len(w.Logs, 4)
// fatal level
w = writer{}
l = logging.New(logging.Fatal, &w)
runLogs(l, assert)
assert.Len(w.Logs, 2)
// write
w = writer{}
l = logging.New(logging.Debug, &w)
l.Write([]byte("write-test"))
assert.Len(w.Logs, 1)
var msg logMessage
assert.Nil(json.Unmarshal([]byte(w.Logs[0]), &msg))
assert.Equal("info", msg.Level)
assert.Equal("write-test", msg.Message)
}
func runLogs(l logging.Logger, assert *assert.Assertions) {
assert.Panics(func() {
l.Fatal("fatal")
})
assert.Panics(func() {
l.ErrFatal(fmt.Errorf("fatal"))
})
l.Error("error")
l.ErrError(fmt.Errorf("error"))
l.Warn("warn")
l.ErrWarn(fmt.Errorf("warn"))
l.Info("info")
l.ErrInfo(fmt.Errorf("info"))
l.Debug("debug")
l.ErrDebug(fmt.Errorf("debug"))
}
type logMessage struct {
Level string `json:"level"`
Time time.Time `json:"time"`
Message string `json:"message"`
}
func testMessage(assert *assert.Assertions, fn func(logging.Logger), value string) {
r, w, err := os.Pipe()
assert.Nil(err)
l := logging.New(logging.Debug, w)
out := make(chan []byte)
go func() {
o, err := ioutil.ReadAll(r)
assert.Nil(err)
fmt.Println(string(o))
out <- o
}()
fn(l)
assert.Nil(w.Close())
var msg logMessage
assert.Nil(json.Unmarshal(<-out, &msg))
assert.Equal(value, msg.Level)
assert.Equal(value, msg.Message)
}