/
options.go
167 lines (140 loc) · 3.69 KB
/
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package logger
import (
"go.uber.org/zap/zapcore"
)
const (
spanKey = "span_id"
traceKey = "trace_id"
callerSkipOffset = 2
)
var (
defaultOption = Option{
level: InfoLevel,
basePath: "./logs",
console: true,
disableDisk: true,
callerSkip: callerSkipOffset,
encoderConfig: defaultEncoderConfig,
fields: make(map[string]interface{}),
encoder: JsonEncoder,
}
defaultEncoderConfig = zapcore.EncoderConfig{
CallerKey: "caller",
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
TimeKey: "ts",
MessageKey: "msg",
LevelKey: "level",
NameKey: "Logger",
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder, // 日期格式改为"ISO8601",例如:"2020-12-16T19:12:48.771+0800"
EncodeDuration: zapcore.StringDurationEncoder,
EncodeName: zapcore.FullNameEncoder,
}
)
type Encoder string
func (e *Encoder) String() string {
return string(*e)
}
// IsJson Whether json encoder.
func (e *Encoder) IsJson() bool {
return e.String() == "json"
}
// IsConsole Whether console encoder.
func (e *Encoder) IsConsole() bool {
return e.String() == "console"
}
const (
JsonEncoder Encoder = "json"
ConsoleEncoder Encoder = "console"
)
type Option struct {
// The logging level the Logger should log at. default is `InfoLevel`
level Level
// basePath defines base path of log file
basePath string
// Logger file name
filename string
// enableConsole display logs to standard output
console bool
// disableDisk disable rolling file
disableDisk bool
callerSkip int
namespace string
fields map[string]interface{}
encoder Encoder
encoderConfig zapcore.EncoderConfig
}
// Level Get log level.
func (o Option) Level() Level {
return o.level
}
type Options func(o *Option)
// WithLevel set base path.
func WithLevel(lv Level) Options {
return func(o *Option) {
o.level = lv
}
}
// WithBasePath set base path.
func WithBasePath(path string) Options {
return func(o *Option) {
o.basePath = path
}
}
// WithFilename Logger filename.
func WithFilename(filename string) Options {
return func(o *Option) {
o.filename = filename
}
}
// WithConsole enable console.
func WithConsole(enableConsole bool) Options {
return func(o *Option) {
o.console = enableConsole
}
}
// WithDisableDisk disable disk.
func WithDisableDisk(disableDisk bool) Options {
return func(o *Option) {
o.disableDisk = disableDisk
}
}
// WithCallerSkip increases the number of callers skipped by caller annotation
// (as enabled by the AddCaller option). When building wrappers around the
// Logger and SugaredLogger, supplying this Option prevents base from always
// reporting the wrapper code as the caller.
func WithCallerSkip(skip int) Options {
return func(o *Option) {
o.callerSkip = skip + callerSkipOffset
}
}
// WithFields set default fields for the logger
func WithFields(fields map[string]interface{}) Options {
return func(o *Option) {
o.fields = fields
}
}
// WithEncoder set logger Encoder
func WithEncoder(encoder Encoder) Options {
return func(o *Option) {
o.encoder = encoder
}
}
// WithEncoderConfig set logger encoderConfig
func WithEncoderConfig(encoderConfig zapcore.EncoderConfig) Options {
return func(o *Option) {
o.encoderConfig = encoderConfig
}
}
// WithNamespace creates a named, isolated scope within the logger's context. All
// subsequent fields will be added to the new namespace.
//
// This helps prevent key collisions when injecting loggers into sub-components
// or third-party libraries.
func WithNamespace(name string) Options {
return func(o *Option) {
o.namespace = name
}
}