-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.go
More file actions
67 lines (58 loc) · 1.39 KB
/
hook.go
File metadata and controls
67 lines (58 loc) · 1.39 KB
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
package logrus_proxy
import "github.com/Sirupsen/logrus"
type Hook struct {
levels map[logrus.Level]bool
hook logrus.Hook
fireFunc func(entry *logrus.Entry)
}
func (hook *Hook) Levels() []logrus.Level {
res := make([]logrus.Level, len(hook.levels))
for lvl := range hook.levels {
res = append(res, lvl)
}
return res
}
func (hook *Hook) SetPreFireFunc(fn func(entry *logrus.Entry)) *Hook {
hook.fireFunc = fn
return hook
}
func (hook *Hook) Fire(entry *logrus.Entry) (err error) {
if enabled, exists := hook.levels[entry.Level]; exists && enabled {
if hook.fireFunc != nil {
hook.fireFunc(entry)
}
err = hook.hook.Fire(entry)
}
return
}
func (hook *Hook) DisableLevel(level logrus.Level) *Hook {
if enabled, exists := hook.levels[level]; exists && enabled {
hook.levels[level] = false
}
return hook
}
func (hook *Hook) EnableLevel(level logrus.Level) *Hook {
if enabled, exists := hook.levels[level]; exists && enabled {
return hook
}
hook.levels[level] = supportsLevel(hook, level)
return hook
}
func NewHook(hook logrus.Hook, levels []logrus.Level) *Hook {
lvls := make(map[logrus.Level]bool)
for _, lvl := range levels {
lvls[lvl] = supportsLevel(hook, lvl)
}
return &Hook{
hook: hook,
levels: lvls,
}
}
func supportsLevel(hook logrus.Hook, level logrus.Level) bool {
for _, lvl := range hook.Levels() {
if lvl == level {
return true
}
}
return false
}