-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
175 lines (153 loc) · 3.92 KB
/
option.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package wzap
import (
"fmt"
"reflect"
"runtime"
"strings"
"github.com/spf13/cast"
"go.uber.org/zap/zapcore"
)
// Option is used to set options for the logger.
type Option func(*Options)
// Options wraps logger options.
type Options struct {
// common options
name string
level int
fs []zapcore.Field
// zap-writer options
path string
// console-writer options
color bool
prefix string
async bool
// added writers
writers []Options
}
// WithName adds name to the logger.
func WithName(name string) Option {
return func(o *Options) {
o.name = name
}
}
// WithFields adds fields to the logger.
func WithFields(fs ...zapcore.Field) Option {
return func(o *Options) {
o.fs = fs
}
}
// WithPath configures logger path.
func WithPath(path string) Option {
return func(o *Options) {
o.path = path
}
}
// WithLevel configures logger minimum level.
func WithLevel(lvFn func(string, ...interface{})) Option {
name := runtime.FuncForPC(reflect.ValueOf(lvFn).Pointer()).Name()
return func(o *Options) {
o.level = minLevel(name)
}
}
func minLevel(minLevel string) (level int) {
lower := strings.ToLower(minLevel)
switch {
case strings.HasSuffix(lower, "debug"):
level = DebugLevel | InfoLevel | WarnLevel | ErrorLevel | PanicLevel | FatalLevel
case strings.HasSuffix(lower, "info"):
level = InfoLevel | WarnLevel | ErrorLevel | PanicLevel | FatalLevel
case strings.HasSuffix(lower, "warn"):
level = WarnLevel | ErrorLevel | PanicLevel | FatalLevel
case strings.HasSuffix(lower, "error"):
level = ErrorLevel | PanicLevel | FatalLevel
case strings.HasSuffix(lower, "panic"):
level = PanicLevel | FatalLevel
case strings.HasSuffix(lower, "fatal"):
level = FatalLevel
}
return
}
func buildOptions(kv map[string]interface{}) (option Options) {
for k, v := range kv {
switch strings.ToLower(k) {
case "name":
option.name = cast.ToString(v)
case "levelcombo":
option.level = parseLevel(cast.ToString(v), "|")
case "level":
option.level = minLevel(cast.ToString(v))
case "path", "file":
option.path = cast.ToString(v)
case "color":
option.color = cast.ToBool(v)
case "prefix":
option.prefix = cast.ToString(v)
case "async":
option.async = cast.ToBool(v)
}
}
return
}
// WithLevelMask configures logger's enabled levels with level masks.
func WithLevelMask(lvMask int) Option {
return func(o *Options) {
o.level = lvMask
}
}
// WithLevelCombo configures logger's enabled levels with level-combo string.
// ex. "Warn | Error | Panic | Fatal" will enable Warn, Error, Panic and Fatal level logging.
func WithLevelCombo(combo string) Option {
return func(o *Options) {
o.level = parseLevel(combo, "|")
}
}
// WithOutput adds a new writer with options.
func WithOutput(opts ...Option) Option {
var options Options
for _, opt := range opts {
opt(&options)
}
return func(o *Options) {
o.writers = append(o.writers, options)
}
}
// WithOutputKV adds a new writer with provided KV.
func WithOutputKV(kv map[string]interface{}) Option {
var option = buildOptions(kv)
return func(o *Options) {
o.writers = append(o.writers, option)
}
}
// WithOutputKVs adds several new writers with provided KVs.
func WithOutputKVs(kvs []interface{}) Option {
var options = make([]Options, 0)
for _, kv := range kvs {
kv, ok := kv.(map[string]interface{})
if !ok {
fmt.Printf("[WithOutputKVs] invalid kv %#v", kv)
continue
}
options = append(options, buildOptions(kv))
}
return func(o *Options) {
o.writers = append(o.writers, options...)
}
}
// WithColorful configures the console-logger's colorful trigger.
func WithColorful(colorful bool) Option {
return func(o *Options) {
o.color = colorful
}
}
// WithPrefix configures the console-logger's prefix.
func WithPrefix(prefix string) Option {
return func(o *Options) {
o.prefix = prefix
}
}
// WithAsync configures the console-logger's async trigger.
func WithAsync(async bool) Option {
return func(o *Options) {
o.async = async
}
}