-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_options.go
150 lines (132 loc) · 2.74 KB
/
log_options.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
package log
import (
"time"
"go.uber.org/zap/zapcore"
)
type level string
type encoder string
// Options uber log
type Options struct {
Encoder encoder
Level level
MessageKey string
LevelKey string
TimeKey string
NameKey string
CallerKey string
StacktraceKey string
LineEnding string
EncodeLevel zapcore.LevelEncoder
EncodeTime zapcore.TimeEncoder
EncodeDuration zapcore.DurationEncoder
EncodeCaller zapcore.CallerEncoder
EncodeName zapcore.NameEncoder
Filename string
MaxSize int
MaxAge int
MaxBackups int
LocalTime bool
Compress bool
}
// Option sugar for options
type Option func(*Options)
var (
DEG level = "debug"
INF level = "info"
WRN level = "warn"
ERR level = "error"
)
var (
JSON encoder = "json"
Console encoder = "console"
)
func newOptions(opts ...Option) (options Options) {
options = Options{
Encoder: Console,
Level: INF,
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
NameKey: "name",
CallerKey: "caller",
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: LevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
MaxSize: 1,
MaxAge: 7,
MaxBackups: 50,
LocalTime: true,
Compress: false,
}
for _, o := range opts {
o(&options)
}
return
}
// Filename log file name
func Filename(name string) Option {
return func(o *Options) {
o.Filename = name
}
}
// Level log level
// default is INF
func Level(l level) Option {
return func(o *Options) {
o.Level = l
}
}
// ParseLevel parse string to level
func ParseLevel(l string) level {
switch l {
case "debug":
return DEG
case "info":
return INF
case "warn":
return WRN
case "error":
return ERR
}
return INF
}
// Encoder choose json/console
// default is console
func Encoder(e encoder) Option {
return func(o *Options) {
o.Encoder = e
}
}
// ParseEncoder parse string to encoder
func ParseEncoder(e string) encoder {
switch e {
case "json":
return JSON
case "console":
return Console
}
return Console
}
// LevelEncoder format level name
func LevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
var level string
switch l {
case zapcore.DebugLevel:
level = "DEG"
case zapcore.InfoLevel:
level = "INF"
case zapcore.WarnLevel:
level = "WRN"
case zapcore.ErrorLevel:
level = "ERR"
}
enc.AppendString(level)
}
// TimeEncoder format time
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}