-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
245 lines (199 loc) · 6.72 KB
/
adapter.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
/*
* Copyright 2023 Enoch <lanxenet@gmail.com>. All rights reserved.
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
package log
import (
"context"
"fmt"
"os"
"sync/atomic"
)
// Option is Helper option.
type Option func(*Adapter)
type Adapter struct {
logger Logger
lvl atomic.Int32
}
// WithLevel with log level.
func WithLevel(level Level) Option {
return func(la *Adapter) {
la.SetLevel(level)
}
}
// NewAdapter new a logger adapter.
func NewAdapter(logger Logger, opts ...Option) *Adapter {
la := &Adapter{
logger: logger,
}
la.SetLevel(InfoLevel)
if opts != nil {
for _, o := range opts {
o(la)
}
}
return la
}
// Level returns the minimum enabled log level.
func (la *Adapter) Level() Level {
return Level(int(la.lvl.Load()))
}
// SetLevel alters the logging level.
func (la *Adapter) SetLevel(l Level) *Adapter {
la.lvl.Store(int32(l))
return la
}
// Enabled implements the zapcore.LevelEnabler interface, which allows the
// AtomicLevel to be used in place of traditional static levels.
func (la *Adapter) Enabled(l Level) bool {
return la.Level().Enabled(l)
}
var ctx = context.Background()
// Print uses fmt.Sprint to construct and logger a message.
func (la *Adapter) Print(args ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprint(args...))
})
}
// Printf uses fmt.Sprintf to logger a templated message.
func (la *Adapter) Printf(msg string, args ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprintf(msg, args...))
})
}
// Printw uses key,value to construct and logger a message.
func (la *Adapter) Printw(keyvals ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
}
// Debug uses fmt.Sprint to construct and logger a message.
func (la *Adapter) Debug(args ...interface{}) {
la.output(ctx, DebugLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprint(args...))
})
}
// Debugf uses fmt.Sprintf to logger a templated message.
func (la *Adapter) Debugf(msg string, args ...interface{}) {
la.output(ctx, DebugLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprintf(msg, args...))
})
}
// Debugw uses key,value to construct and logger a message.
func (la *Adapter) Debugw(keyvals ...interface{}) {
la.output(ctx, DebugLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
}
// Info uses fmt.Sprint to construct and logger a message.
func (la *Adapter) Info(args ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprint(args...))
})
}
// Infof uses fmt.Sprintf to logger a templated message.
func (la *Adapter) Infof(msg string, args ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, sprintf(msg, args...))
})
}
// Infow uses key,value to construct and logger a message.
func (la *Adapter) Infow(keyvals ...interface{}) {
la.output(ctx, InfoLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
}
// Warn uses fmt.Sprint to construct and logger a message.
func (la *Adapter) Warn(args ...interface{}) {
la.output(ctx, WarnLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprint(args...))
})
}
// Warnf uses fmt.Sprintf to logger a templated message.
func (la *Adapter) Warnf(msg string, args ...interface{}) {
la.output(ctx, WarnLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprintf(msg, args...))
})
}
// Warnw uses key,value to construct and logger a message.
func (la *Adapter) Warnw(keyvals ...interface{}) {
la.output(ctx, WarnLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
}
// Error uses fmt.Sprint to construct and logger a message.
func (la *Adapter) Error(args ...interface{}) {
la.output(ctx, ErrorLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprint(args...))
})
}
// Errorf uses fmt.Sprintf to logger a templated message.
func (la *Adapter) Errorf(msg string, args ...interface{}) {
la.output(ctx, ErrorLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprintf(msg, args...))
})
}
// Errorw uses key,value to construct and logger a message.
func (la *Adapter) Errorw(keyvals ...interface{}) {
la.output(ctx, ErrorLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
}
// Panic uses fmt.Sprint to construct and logger a message, then panics.
func (la *Adapter) Panic(args ...interface{}) {
la.output(ctx, PanicLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprint(args...))
})
panic(fmt.Sprint(args...))
}
// Panicf uses fmt.Sprintf to logger a templated message, then panics.
func (la *Adapter) Panicf(msg string, args ...interface{}) {
la.output(ctx, PanicLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprintf(msg, args...))
})
panic(fmt.Sprintf(msg, args...))
}
// Panicw uses key,value to construct and logger a message.
func (la *Adapter) Panicw(keyvals ...interface{}) {
la.output(ctx, PanicLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
panic(fmt.Sprint(keyvals...))
}
// Fatal uses fmt.Sprint to construct and logger a message, then calls os.Exit.
func (la *Adapter) Fatal(args ...interface{}) {
la.output(ctx, FatalLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprint(args...))
})
os.Exit(1)
}
// Fatalf uses fmt.Sprintf to logger a templated message, then calls os.Exit.
func (la *Adapter) Fatalf(msg string, args ...interface{}) {
la.output(ctx, FatalLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", sprintf(msg, args...))
})
os.Exit(1)
}
// Fatalw uses key,value to construct and logger a message.
func (la *Adapter) Fatalw(keyvals ...interface{}) {
la.output(ctx, FatalLevel, func(ctx context.Context, level Level) {
la.logger.Log(ctx, level, "", keyvals...)
})
os.Exit(1)
}
// Flush flushing any buffered log entries. Applications should take care to call Sync before exiting.
func (la *Adapter) Flush() error {
return la.Close()
}
func (la *Adapter) Close() error {
return la.logger.Close()
}
func (la *Adapter) Log(_ context.Context, level Level, msg string, keyValues ...interface{}) {
la.logger.Log(ctx, level, msg, keyValues...)
}
func (la *Adapter) output(ctx context.Context, l Level, log func(ctx context.Context, level Level)) {
if la.Enabled(l) {
log(ctx, l)
}
}