-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfilters.go
58 lines (49 loc) · 1.14 KB
/
filters.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
// Copyright (C) 2017, ccpaging <ccpaging@gmail.com>. All rights reserved.
package nxlog4go
import (
"github.com/ccpaging/nxlog4go/driver"
)
// AddFilter adds the named appenders to the Logger which will only log messages at
// or above level. This function should not be called from multiple goroutines.
//
// Returns the logger for chaining.
func (l *Logger) AddFilter(name string, level int, apps ...driver.Appender) *Logger {
f := &driver.Filter{
Name: name,
Enabler: driver.AtAbove(level),
Layout: nil,
Apps: apps,
}
return l.Attach(f)
}
// Attach adds the filters to logger.
func (l *Logger) Attach(filters ...*driver.Filter) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
for _, f := range filters {
if f == nil {
continue
}
if _, ok := l.filters[f.Name]; ok {
// Existed
continue
}
l.filters[f.Name] = f
}
return l
}
// Detach removes the filters from logger.
func (l *Logger) Detach(filters ...*driver.Filter) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
for _, f := range filters {
if f == nil {
continue
}
if _, ok := l.filters[f.Name]; ok {
// Existed
delete(l.filters, f.Name)
}
}
return l
}