forked from torbensky/go-logfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hookfilter.go
52 lines (43 loc) · 953 Bytes
/
hookfilter.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
package logfilter
import (
"runtime"
"strings"
log "github.com/sirupsen/logrus"
)
type HookFilter struct {
Filter *LogFilter
hook log.Hook
}
func NewHookFilter(hook log.Hook, filter *LogFilter) *HookFilter {
return &HookFilter{
hook: hook,
Filter: filter,
}
}
func (hf HookFilter) Levels() []log.Level {
return log.AllLevels
}
func (hf HookFilter) Fire(entry *log.Entry) error {
// Search the callstack for the first non-logrus file. This should be where the log call originated from.
skip := 1
for {
_, file, _, ok := runtime.Caller(skip)
if !ok {
break
}
if !strings.Contains(file, "sirupsen/logrus") && !strings.Contains(file, "<autogenerated>") {
if hf.ShouldLog(entry, file) {
return hf.hook.Fire(entry)
}
break
}
skip++
if skip > 100 {
break
}
}
return nil
}
func (hf HookFilter) ShouldLog(entry *log.Entry, file string) bool {
return hf.Filter.GetFileLevel(file) >= entry.Level
}