-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
218 lines (166 loc) · 3.63 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package log
import (
"path"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
_logSuffix = ".log"
)
type Option zap.Option
type options struct {
level Level
debug bool
localTime bool
tees []teeOption
skip int
traceKey string
hooks []func(zapcore.Entry) error
opts []zap.Option
}
type LevelEnablerFunc func(Level) zap.LevelEnablerFunc
type teeOption struct {
file string // path+filename
levelFunc LevelEnablerFunc
ratate rotateOption
}
type rotateOption struct {
compress bool
maxSize int
maxAge int
maxBackups int
}
var (
defTraceKey = "@trace_id"
defLevelFunc = func(level Level) zap.LevelEnablerFunc {
return func(l zapcore.Level) bool {
return l >= level
}
}
defRotateOpts = rotateOption{
compress: false,
maxSize: 100,
maxAge: 7,
maxBackups: 10,
}
defTeeOpts = teeOption{
file: "./log/server.log",
levelFunc: defLevelFunc,
ratate: defRotateOpts,
}
defOpts = options{
level: InfoLevel,
localTime: true,
traceKey: defTraceKey,
}
)
func evalOptions(opts ...Options) *options {
optCopy := defOpts
for _, o := range opts {
o.apply(&optCopy)
}
return &optCopy
}
type Options interface {
apply(*options)
}
func WithLevel(l Level) Options {
return levelOption(l)
}
func WithDebug(debug bool) Options {
return debugOption(debug)
}
func WithLocalTime(localTime bool) Options {
return localTimeOption(localTime)
}
func WithSkip(skip int) Options {
return skipOption(skip)
}
func WithTraceKey(traceKey string) Options {
return tracekeyOption(traceKey)
}
func WithHooks(hooks []func(zapcore.Entry) error) Options {
return hooksOption(hooks)
}
func WithOptions(opts []zap.Option) Options {
return optOptions(opts)
}
type fileOption interface {
apply(*rotateOption)
}
func WithFile(file string, f LevelEnablerFunc, opts ...fileOption) Options {
if path.Ext(file) != _logSuffix {
file += _logSuffix
}
rotp := defRotateOpts
for _, o := range opts {
o.apply(&rotp)
}
tee := teeOption{
file: file,
levelFunc: f,
ratate: rotp,
}
return teeOptions(tee)
}
func WithMaxSize(maxSize int) fileOption {
return maxSizeOptions(maxSize)
}
func WithMaxAge(age int) fileOption {
return maxAgeOptions(age)
}
func WithMaxBackups(backup int) fileOption {
return maxBackupsOptions(backup)
}
func WithCompress(compress bool) fileOption {
return compressOptions(compress)
}
// internal
type levelOption Level
func (l levelOption) apply(opts *options) {
opts.level = Level(l)
}
type debugOption bool
func (d debugOption) apply(opts *options) {
opts.debug = bool(d)
}
type localTimeOption bool
func (lt localTimeOption) apply(opts *options) {
opts.localTime = bool(lt)
}
type skipOption int
func (s skipOption) apply(opts *options) {
opts.skip = int(s)
}
type tracekeyOption string
func (t tracekeyOption) apply(opts *options) {
opts.traceKey = string(t)
}
type hooksOption []func(zapcore.Entry) error
func (h hooksOption) apply(opts *options) {
opts.hooks = []func(zapcore.Entry) error(h)
}
type optOptions []zap.Option
func (o optOptions) apply(opts *options) {
opts.opts = optOptions(o)
}
type teeOptions teeOption
func (t teeOptions) apply(opts *options) {
opts.tees = append(opts.tees, teeOption(t))
}
type maxSizeOptions int
func (m maxSizeOptions) apply(opts *rotateOption) {
opts.maxSize = int(m)
}
type maxAgeOptions int
func (m maxAgeOptions) apply(opts *rotateOption) {
opts.maxAge = int(m)
}
type maxBackupsOptions int
func (m maxBackupsOptions) apply(opts *rotateOption) {
opts.maxBackups = int(m)
}
type compressOptions bool
func (c compressOptions) apply(opts *rotateOption) {
opts.compress = bool(c)
}