forked from hhkbp2/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
100 lines (88 loc) · 2.74 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package logging
import (
"strings"
)
// Filter interface is to perform arbitrary filtering of LogRecords.
// Loggers and handlers can optionally use filter instances to filter records
// as desired.
type Filter interface {
Filter(record *LogRecord) bool
}
// The base filter allows events which are below a certain point in the logger
// hierarchy. For Example, a filter initialized with "A.B" will allow events
// logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB",
// "B.A.B" etc. If initialized with the empty string, all events are passed.
type NameFilter struct {
name string
}
// Initialize a name filter.
// The name of the logger/handler is specified, all the events of
// logger's children are allowed through the filter. If no name is specified,
// every event is allowed.
func NewNameFilter(name string) *NameFilter {
return &NameFilter{
name: name,
}
}
// Determine if the specified record is to be logged.
// Is the specified record to be logged? Returns false for no, true for yes.
// If deemed appropriate, the record may be modified in-place.
func (self *NameFilter) Filter(record *LogRecord) bool {
length := len(self.name)
if length == 0 {
return true
} else if self.name == record.Name {
return true
} else if !strings.HasPrefix(record.Name, self.name) {
return false
}
return (record.Name[length] == '.')
}
// An interface for managing filters.
type Filterer interface {
AddFilter(filter Filter)
RemoveFilter(filter Filter)
Filter(record *LogRecord) int
}
// An base class for loggers and handlers which allows them to share common code
// of managing the filters.
type StandardFilterer struct {
filters *ListSet
}
// Initialize the standard filterer, with no filter.
func NewStandardFilterer() *StandardFilterer {
return &StandardFilterer{
filters: NewListSet(),
}
}
// Add the specified filter.
func (self *StandardFilterer) AddFilter(filter Filter) {
if !self.filters.SetContains(filter) {
self.filters.SetAdd(filter)
}
}
// Remove the specified filter.
func (self *StandardFilterer) RemoveFilter(filter Filter) {
if self.filters.SetContains(filter) {
self.filters.SetRemove(filter)
}
}
// Determine if a record is loggable by consulting all the filters.
// The default is to allow the record to be logged: any filter can veto
// this and the record is then dropped. Returns a zero value if a record
// is to be dropped, else non-zero.
func (self *StandardFilterer) Filter(record *LogRecord) int {
recordVote := 1
for e := self.filters.Front(); e != nil; e = e.Next() {
filter, _ := e.Value.(Filter)
if !filter.Filter(record) {
recordVote = 0
break
}
}
return recordVote
}
// Return all the filter in this filterer.
func (self *StandardFilterer) GetFilters() *ListSet {
return self.filters
}