-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzap.go
253 lines (209 loc) · 5.54 KB
/
zap.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package zap // import "go.unistack.org/micro-logger-zap/v3"
import (
"context"
"fmt"
"os"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.unistack.org/micro/v3/logger"
)
type zaplog struct {
zap *zap.Logger
opts logger.Options
sync.RWMutex
}
func (l *zaplog) Level(lvl logger.Level) {
}
func (l *zaplog) Clone(opts ...logger.Option) logger.Logger {
nl := &zaplog{zap: l.zap, opts: l.opts}
for _, o := range opts {
o(&nl.opts)
}
return nl
}
func (l *zaplog) Init(opts ...logger.Option) error {
var err error
for _, o := range opts {
o(&l.opts)
}
if zlog, ok := l.opts.Context.Value(loggerKey{}).(*zap.Logger); ok {
l.zap = zlog
return nil
}
zapConfig := zap.NewProductionConfig()
if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
zapConfig = zconfig
}
if zcconfig, ok := l.opts.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok {
zapConfig.EncoderConfig = zcconfig
}
skip, ok := l.opts.Context.Value(callerSkipKey{}).(int)
if !ok || skip < 1 {
skip = 1
}
// Set log Level if not default
zapConfig.Level = zap.NewAtomicLevel()
if l.opts.Level != logger.InfoLevel {
zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
}
log, err := zapConfig.Build(zap.AddCallerSkip(skip))
if err != nil {
return err
}
log = log.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewCore(
zapcore.NewJSONEncoder(zapConfig.EncoderConfig),
zapcore.Lock(zapcore.AddSync(l.opts.Out)),
loggerToZapLevel(l.opts.Level),
)
}))
// Adding seed fields if exist
if l.opts.Fields != nil {
data := make([]zap.Field, 0, len(l.opts.Fields)/2)
for i := 0; i < len(l.opts.Fields); i += 2 {
data = append(data, zap.Any(l.opts.Fields[i].(string), l.opts.Fields[i+1]))
}
log = log.With(data...)
}
// Adding namespace
if namespace, ok := l.opts.Context.Value(namespaceKey{}).(string); ok {
log = log.With(zap.Namespace(namespace))
}
// defer log.Sync() ??
l.zap = log
return nil
}
func (l *zaplog) Fields(fields ...interface{}) logger.Logger {
data := make([]zap.Field, 0, len(fields)/2)
for i := 0; i < len(fields); i += 2 {
data = append(data, zap.Any(fields[i].(string), fields[i+1]))
}
zl := &zaplog{
zap: l.zap.With(data...),
opts: l.opts,
}
return zl
}
func (l *zaplog) Errorf(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.ErrorLevel, msg, args...)
}
func (l *zaplog) Debugf(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.DebugLevel, msg, args...)
}
func (l *zaplog) Infof(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.InfoLevel, msg, args...)
}
func (l *zaplog) Fatalf(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.FatalLevel, msg, args...)
os.Exit(1)
}
func (l *zaplog) Tracef(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.TraceLevel, msg, args...)
}
func (l *zaplog) Warnf(ctx context.Context, msg string, args ...interface{}) {
l.Logf(ctx, logger.WarnLevel, msg, args...)
}
func (l *zaplog) Error(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.ErrorLevel, args...)
}
func (l *zaplog) Debug(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.DebugLevel, args...)
}
func (l *zaplog) Info(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.InfoLevel, args...)
}
func (l *zaplog) Fatal(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.FatalLevel, args...)
os.Exit(1)
}
func (l *zaplog) Trace(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.TraceLevel, args...)
}
func (l *zaplog) Warn(ctx context.Context, args ...interface{}) {
l.Log(ctx, logger.WarnLevel, args...)
}
func (l *zaplog) Log(ctx context.Context, level logger.Level, args ...interface{}) {
if !l.V(level) {
return
}
msg := fmt.Sprint(args...)
switch loggerToZapLevel(level) {
case zap.DebugLevel:
l.zap.Debug(msg)
case zap.InfoLevel:
l.zap.Info(msg)
case zap.WarnLevel:
l.zap.Warn(msg)
case zap.ErrorLevel:
l.zap.Error(msg)
case zap.FatalLevel:
l.zap.Fatal(msg)
}
}
func (l *zaplog) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) {
if !l.V(level) {
return
}
msg := fmt.Sprintf(format, args...)
switch loggerToZapLevel(level) {
case zap.DebugLevel:
l.zap.Debug(msg)
case zap.InfoLevel:
l.zap.Info(msg)
case zap.WarnLevel:
l.zap.Warn(msg)
case zap.ErrorLevel:
l.zap.Error(msg)
case zap.FatalLevel:
l.zap.Fatal(msg)
}
}
func (l *zaplog) V(level logger.Level) bool {
return l.zap.Core().Enabled(loggerToZapLevel(level))
}
func (l *zaplog) String() string {
return "zap"
}
func (l *zaplog) Options() logger.Options {
return l.opts
}
// New builds a new logger based on options
func NewLogger(opts ...logger.Option) logger.Logger {
l := &zaplog{opts: logger.NewOptions(opts...)}
return l
}
func loggerToZapLevel(level logger.Level) zapcore.Level {
switch level {
case logger.TraceLevel, logger.DebugLevel:
return zap.DebugLevel
case logger.InfoLevel:
return zap.InfoLevel
case logger.WarnLevel:
return zap.WarnLevel
case logger.ErrorLevel:
return zap.ErrorLevel
case logger.FatalLevel:
return zap.FatalLevel
default:
return zap.InfoLevel
}
}
/*
func zapToLoggerLevel(level zapcore.Level) logger.Level {
switch level {
case zap.DebugLevel:
return logger.DebugLevel
case zap.InfoLevel:
return logger.InfoLevel
case zap.WarnLevel:
return logger.WarnLevel
case zap.ErrorLevel:
return logger.ErrorLevel
case zap.FatalLevel:
return logger.FatalLevel
default:
return logger.InfoLevel
}
}
*/