forked from phuslu/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
140 lines (120 loc) · 3.03 KB
/
multi.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
package log
import (
"io"
)
// MultiWriter is an alias for MultiLevelWriter
type MultiWriter = MultiLevelWriter
// MultiLevelWriter is an Writer that log to different writers by different levels
type MultiLevelWriter struct {
// InfoWriter specifies all the level logs writes to
InfoWriter Writer
// WarnWriter specifies the level greater than or equal to WarnLevel writes to
WarnWriter Writer
// WarnWriter specifies the level greater than or equal to ErrorLevel writes to
ErrorWriter Writer
// ConsoleWriter specifies the console writer
ConsoleWriter Writer
// ConsoleLevel specifies the level greater than or equal to it also writes to
ConsoleLevel Level
}
// Close implements io.Closer, and closes the underlying LeveledWriter.
func (w *MultiLevelWriter) Close() (err error) {
for _, writer := range []Writer{
w.InfoWriter,
w.WarnWriter,
w.ErrorWriter,
w.ConsoleWriter,
} {
if writer == nil {
continue
}
if closer, ok := writer.(io.Closer); ok {
if err1 := closer.Close(); err1 != nil {
err = err1
}
}
}
return
}
// WriteEntry implements entryWriter.
func (w *MultiLevelWriter) WriteEntry(e *Entry) (n int, err error) {
var err1 error
switch e.Level {
case noLevel, PanicLevel, FatalLevel, ErrorLevel:
if w.ErrorWriter != nil {
n, err1 = w.ErrorWriter.WriteEntry(e)
if err1 != nil && err == nil {
err = err1
}
}
fallthrough
case WarnLevel:
if w.WarnWriter != nil {
n, err1 = w.WarnWriter.WriteEntry(e)
if err1 != nil && err == nil {
err = err1
}
}
fallthrough
default:
if w.InfoWriter != nil {
n, err1 = w.InfoWriter.WriteEntry(e)
if err1 != nil && err == nil {
err = err1
}
}
}
if w.ConsoleWriter != nil && e.Level >= w.ConsoleLevel {
_, _ = w.ConsoleWriter.WriteEntry(e)
}
return
}
var _ Writer = (*MultiLevelWriter)(nil)
// MultiEntryWriter is an array Writer that log to different writers
type MultiEntryWriter []Writer
// Close implements io.Closer, and closes the underlying MultiEntryWriter.
func (w *MultiEntryWriter) Close() (err error) {
for _, writer := range *w {
if closer, ok := writer.(io.Closer); ok {
if err1 := closer.Close(); err1 != nil {
err = err1
}
}
}
return
}
// WriteEntry implements entryWriter.
func (w *MultiEntryWriter) WriteEntry(e *Entry) (n int, err error) {
var err1 error
for _, writer := range *w {
n, err1 = writer.WriteEntry(e)
if err1 != nil && err == nil {
err = err1
}
}
return
}
var _ Writer = (*MultiEntryWriter)(nil)
// MultiIOWriter is an array io.Writer that log to different writers
type MultiIOWriter []io.Writer
// Close implements io.Closer, and closes the underlying MultiIOWriter.
func (w *MultiIOWriter) Close() (err error) {
for _, writer := range *w {
if closer, ok := writer.(io.Closer); ok {
if err1 := closer.Close(); err1 != nil {
err = err1
}
}
}
return
}
// WriteEntry implements entryWriter.
func (w *MultiIOWriter) WriteEntry(e *Entry) (n int, err error) {
for _, writer := range *w {
n, err = writer.Write(e.buf)
if err != nil {
return
}
}
return
}