-
Notifications
You must be signed in to change notification settings - Fork 45
/
multi_test.go
188 lines (160 loc) · 5.95 KB
/
multi_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package log
import (
"errors"
"io"
"os"
"path/filepath"
"testing"
)
func TestMultiWriter(t *testing.T) {
w := &MultiWriter{
InfoWriter: &FileWriter{Filename: "file-info.log"},
WarnWriter: &FileWriter{Filename: "file-warn.log"},
ErrorWriter: &FileWriter{Filename: "file-error.log"},
ConsoleWriter: &ConsoleWriter{ColorOutput: true},
ConsoleLevel: ErrorLevel,
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, err := wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
}
if err := w.Close(); err != nil {
t.Errorf("test close mutli writer error: %+v", err)
}
matches, _ := filepath.Glob("file-*.*.log")
for i := range matches {
err := os.Remove(matches[i])
if err != nil {
t.Fatalf("os remove %s error: %+v", matches[i], err)
}
}
}
type errorWriter struct {
io.WriteCloser
}
var errorWriterOK = errors.New("errorWriter return OK")
func (ew errorWriter) Write(p []byte) (n int, err error) {
n, _ = ew.WriteCloser.Write(p)
err = errorWriterOK
return
}
func (ew errorWriter) Close() (err error) {
ew.WriteCloser.Close()
err = errorWriterOK
return
}
func TestMultiWriterError(t *testing.T) {
file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
if err != nil {
t.Errorf("open null file error: %+v", err)
}
w := &MultiWriter{
InfoWriter: IOWriter{errorWriter{file}},
WarnWriter: IOWriter{errorWriter{file}},
ErrorWriter: IOWriter{errorWriter{file}},
ConsoleWriter: &ConsoleWriter{ColorOutput: true},
ConsoleLevel: TraceLevel,
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, err := wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err == nil {
t.Errorf("test json error writer error: %+v", err)
}
}
if err := w.Close(); err != nil {
t.Errorf("test close error writer error: %+v", err)
}
}
func TestMultiWriterEntry(t *testing.T) {
w := &MultiWriter{
ConsoleWriter: &ConsoleWriter{
ColorOutput: true,
},
ConsoleLevel: InfoLevel,
}
var err error
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
}
if err := w.Close(); err != nil {
t.Errorf("test close mutli writer error: %+v", err)
}
}
func TestMultiEntryWriter(t *testing.T) {
w := &MultiEntryWriter{
&FileWriter{Filename: "file-1.log"},
&FileWriter{Filename: "file-2.log"},
&ConsoleWriter{ColorOutput: true},
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, err := wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err != nil {
t.Errorf("test json mutli writer error: %+v", err)
}
}
if err := w.Close(); err != nil {
t.Errorf("test close mutli writer error: %+v", err)
}
matches, _ := filepath.Glob("file-*.*.log")
for i := range matches {
err := os.Remove(matches[i])
if err != nil {
t.Fatalf("os remove %s error: %+v", matches[i], err)
}
}
}
type errorEntryWriter struct {
io.WriteCloser
}
var errorEntryWriterOK = errors.New("errorEntryWriter return OK")
func (ew errorEntryWriter) Write(p []byte) (n int, err error) {
n, _ = ew.WriteCloser.Write(p)
err = errorEntryWriterOK
return
}
func (ew errorEntryWriter) Close() (err error) {
ew.WriteCloser.Close()
err = errorEntryWriterOK
return
}
func TestMultiEntryWriterError(t *testing.T) {
file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
if err != nil {
t.Errorf("open null file error: %+v", err)
}
w := &MultiEntryWriter{
IOWriter{errorEntryWriter{file}},
IOWriter{errorEntryWriter{file}},
&ConsoleWriter{ColorOutput: true},
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, err := wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
if err == nil {
t.Errorf("test json error writer error: %+v", err)
}
}
if err := w.Close(); err != nil {
t.Errorf("test close error writer error: %+v", err)
}
}