-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
44 lines (41 loc) · 838 Bytes
/
util.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
package notify
import (
"github.com/apex/log"
)
// AsyncLogger receives notification entries from a channel and logs them
func AsyncLogger() (chan<- *Entry, chan bool) {
c := make(chan *Entry)
d := make(chan bool, 1)
go func(c <-chan *Entry, d chan<- bool) {
defer func() {
d <- true
}()
for e := range c {
ctx := log.WithFields(log.Fields(e.Fields))
switch e.Level {
case LevelDebug:
ctx.Debug(e.Message)
case LevelInfo:
ctx.Info(e.Message)
case LevelWarn:
ctx.Warn(e.Message)
case LevelError:
ctx.Error(e.Message)
case LevelFatal:
ctx.Fatal(e.Message)
default:
ctx.Error(e.Message)
}
}
}(c, d)
return c, d
}
// AsyncDiscard discards all notifications
func AsyncDiscard() chan<- *Entry {
c := make(chan *Entry)
go func() {
for _ = range c {
}
}()
return c
}