forked from davyxu/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
58 lines (41 loc) · 876 Bytes
/
manager.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
package golog
import (
"regexp"
"sync"
)
var (
loggerByName = map[string]*Logger{}
loggerByNameGuard sync.RWMutex
)
func add(l *Logger) {
loggerByNameGuard.Lock()
if l.name != "" {
if _, ok := loggerByName[l.name]; ok {
panic("duplicate logger name:" + l.name)
}
loggerByName[l.name] = l
}
loggerByNameGuard.Unlock()
}
// 支持正则表达式查找logger, a|b|c指定多个日志, .表示所有日志
func VisitLogger(names string, callback func(*Logger) bool) error {
loggerByNameGuard.RLock()
defer loggerByNameGuard.RUnlock()
exp, err := regexp.Compile(names)
if err != nil {
return err
}
for _, l := range loggerByName {
if exp.MatchString(l.Name()) {
if !callback(l) {
break
}
}
}
return nil
}
func ClearAll() {
loggerByNameGuard.Lock()
loggerByName = map[string]*Logger{}
loggerByNameGuard.Unlock()
}