-
Notifications
You must be signed in to change notification settings - Fork 41
/
hooks.go
61 lines (48 loc) · 1.16 KB
/
hooks.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
package logrus_mate
import (
"errors"
"sort"
"sync"
"github.com/gogap/config"
"github.com/sirupsen/logrus"
)
var (
hooksLocker = sync.Mutex{}
newHookFuncs = make(map[string]NewHookFunc)
)
type NewHookFunc func(config.Configuration) (hook logrus.Hook, err error)
func RegisterHook(name string, newHookFunc NewHookFunc) {
hooksLocker.Lock()
defer hooksLocker.Unlock()
if name == "" {
panic("logurs mate: Register hook name is empty")
}
if newHookFunc == nil {
panic("logurs mate: Register hook is nil")
}
if _, exist := newHookFuncs[name]; exist {
panic("logurs mate: Register called twice for hook " + name)
}
newHookFuncs[name] = newHookFunc
}
func Hooks() []string {
hooksLocker.Lock()
defer hooksLocker.Unlock()
var list []string
for name := range newHookFuncs {
list = append(list, name)
}
sort.Strings(list)
return list
}
func NewHook(name string, config config.Configuration) (hook logrus.Hook, err error) {
hooksLocker.Lock()
defer hooksLocker.Unlock()
if newHookFunc, exist := newHookFuncs[name]; !exist {
err = errors.New("logurs mate: hook not registerd: " + name)
return
} else {
hook, err = newHookFunc(config)
}
return
}