-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.go
More file actions
47 lines (39 loc) · 953 Bytes
/
Copy pathcolor.go
File metadata and controls
47 lines (39 loc) · 953 Bytes
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
package golog
import (
"sync"
"github.com/fatih/color"
)
var logColor *levelColors
type levelColors struct {
attrs map[Level][]color.Attribute
mu *sync.RWMutex
}
// 初始化颜色
func init() {
logColor = &levelColors{
mu: &sync.RWMutex{},
attrs: make(map[Level][]color.Attribute),
}
SetColor(ERROR, []color.Attribute{color.FgRed})
SetColor(WARN, []color.Attribute{color.FgYellow})
SetColor(DEBUG, []color.Attribute{color.FgGreen})
}
// 设置某级别的颜色
func SetColor(lv Level, attrs []color.Attribute) {
logColor.mu.Lock()
logColor.attrs[lv] = attrs
logColor.mu.Unlock()
}
func GetColor(lv Level) []color.Attribute {
logColor.mu.RLock()
defer logColor.mu.RUnlock()
if attrs, ok := logColor.attrs[lv]; ok {
return attrs
}
return nil
}
func CleanColor(lv Level, attrs []color.Attribute) {
logColor.mu.Lock()
delete(logColor.attrs, lv)
logColor.mu.Unlock()
}