-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
31 lines (27 loc) · 842 Bytes
/
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
package golog
type multiLogger struct {
loggers []Logger
}
func (t *multiLogger) Log(level Level, kvs ...interface{}) {
for _, l := range t.loggers {
l.Log(level, kvs...)
}
}
var _ Logger = (*multiLogger)(nil)
// MultiLogger creates a logger that duplicates its logs to all the
// provided loggers, similar to the Unix tee(1) command.
//
// Each log is logged to each listed logger, one at a time.
// If a listed logger returns an error, that overall log operation
// stops and returns the error; it does not continue down the list.
func MultiLogger(loggers ...Logger) Logger {
allLoggers := make([]Logger, 0, len(loggers))
for _, l := range loggers {
if ml, ok := l.(*multiLogger); ok {
allLoggers = append(allLoggers, ml.loggers...)
} else {
allLoggers = append(allLoggers, l)
}
}
return &multiLogger{allLoggers}
}