-
Notifications
You must be signed in to change notification settings - Fork 1
/
multihandler.go
60 lines (52 loc) · 1.07 KB
/
multihandler.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
package log
import (
"sync"
"github.com/hashicorp/go-multierror"
)
// MultiHandler sends the log output to multiple handlers concurrently.
type MultiHandler struct {
handlers []Handler
}
func NewMultiHandler(handlers ...Handler) *MultiHandler {
return &MultiHandler{handlers: handlers}
}
func (b *MultiHandler) SetFormatter(f Formatter) {
for _, h := range b.handlers {
h.SetFormatter(f)
}
}
func (b *MultiHandler) SetLevel(l Level) {
for _, h := range b.handlers {
h.SetLevel(l)
}
}
func (b *MultiHandler) Handle(rec *Record) {
wg := sync.WaitGroup{}
wg.Add(len(b.handlers))
for _, handler := range b.handlers {
go func(handler Handler) {
handler.Handle(rec)
wg.Done()
}(handler)
}
wg.Wait()
}
func (b *MultiHandler) Close() error {
var result error
var m sync.Mutex
wg := sync.WaitGroup{}
wg.Add(len(b.handlers))
for _, handler := range b.handlers {
go func(handler Handler) {
err := handler.Close()
m.Lock()
if err != nil {
result = multierror.Append(result, err)
}
m.Unlock()
wg.Done()
}(handler)
}
wg.Wait()
return result
}