forked from go-ozzo/ozzo-log
-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
78 lines (70 loc) · 1.85 KB
/
filter.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2015 Qiang Xue. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"strings"
)
// Filter checks if a log message meets the level and category requirements.
type Filter struct {
catNames map[string]bool
catPrefixes []string
MaxLevel Leveler // the maximum severity level that is allowed
Levels map[Leveler]bool // 此属性被设置时,MaxLevel 无效
Categories []string // the allowed message categories. Categories can use "*" as a suffix for wildcard matching.
}
// Init initializes the filter.
// Init must be called before Allow is called.
func (t *Filter) Init() {
t.catNames = make(map[string]bool, 0)
t.catPrefixes = make([]string, 0)
for _, cat := range t.Categories {
if strings.HasSuffix(cat, "*") {
t.catPrefixes = append(t.catPrefixes, cat[:len(cat)-1])
} else {
t.catNames[cat] = true
}
}
if t.Levels != nil {
t.MaxLevel = Level(-1)
}
}
// Allow checks if a message meets the severity level and category requirements.
func (t *Filter) Allow(e *Entry) bool {
if e == nil {
return true
}
if t.MaxLevel.Int() > -1 {
if e.Level.Int() > t.MaxLevel.Int() {
return false
}
} else {
if t.Levels == nil || !t.Levels[e.Level] {
return false
}
}
if t.catNames[e.Category] {
return true
}
for _, cat := range t.catPrefixes {
if strings.HasPrefix(e.Category, cat) {
return true
}
}
return len(t.catNames) == 0 && len(t.catPrefixes) == 0
}
func (t *Filter) SetLevel(level interface{}) {
if name, ok := level.(string); ok {
if le, ok := GetLevel(name); ok {
t.MaxLevel = le
}
} else if id, ok := level.(Leveler); ok {
t.MaxLevel = id
}
}
func (t *Filter) SetLevels(levels ...Leveler) {
t.Levels = map[Leveler]bool{}
for _, level := range levels {
t.Levels[level] = true
}
}