-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
95 lines (78 loc) · 1.9 KB
/
writer.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
package logger
import "fmt"
type encoder interface {
Print(l Level, s string, caller string, stack []string, messages []interface{})
Prints(l Level, s string, caller string, stack []string, message string)
Printf(l Level, s string, caller string, stack []string, format string, args []interface{})
Printv(l Level, s string, caller string, stack []string, message string, keysValues []interface{})
Close()
}
type Writer struct {
encoder
caller bool
isEnable EnablerFunc
isStack Level
}
func NewWriter(caller bool, encoder encoder) *Writer {
w := &Writer{
encoder: encoder,
caller: caller,
isStack: ErrorLevel,
}
w.Enabler(func(Level, string) bool { return true })
return w
}
func (w *Writer) Config(scope map[string]string, level *string, stack *string) error {
def := TraceLevel
stackLevel := ErrorLevel
if level != nil {
var ok bool
def, ok = ToLevel(*level)
if !ok {
return fmt.Errorf("config value is illegal: %s", *level)
}
}
if stack != nil {
var ok bool
stackLevel, ok = ToLevel(*stack)
if !ok {
return fmt.Errorf("config value is illegal: %s", *stack)
}
}
if scope != nil {
r := make(map[string]Level)
for key, value := range scope {
l, ok := ToLevel(value)
if !ok {
return fmt.Errorf("config value is illegal: %s:%s", key, value)
}
r[key] = l
}
w.EnablerByScope(r, def)
} else {
w.EnablerByLevel(def)
}
w.StackByLevel(stackLevel)
return nil
}
func (w *Writer) Enabler(fn EnablerFunc) *Writer {
w.isEnable = fn
return w
}
func (w *Writer) EnablerByLevel(l Level) *Writer {
return w.Enabler(func(level Level, _ string) bool {
return level >= l
})
}
func (w *Writer) EnablerByScope(m map[string]Level, def Level) *Writer {
return w.Enabler(func(level Level, scope string) bool {
if l, ok := m[scope]; ok {
return level >= l
}
return level >= def
})
}
func (w *Writer) StackByLevel(l Level) *Writer {
w.isStack = l
return w
}