This repository was archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
362 lines (324 loc) · 8.49 KB
/
context.go
File metadata and controls
362 lines (324 loc) · 8.49 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package log
import (
"fmt"
"reflect"
"runtime"
"strconv"
"sync"
"time"
)
// Context holds context ctx
type Context struct {
logger *Logger
level Level
prefix string
encoder encoder
}
var ctxPool = sync.Pool{
New: func() interface{} {
return new(Context)
},
}
func getContext(logger *Logger, level Level, prefix string) *Context {
if logger == nil || logger.GetLevel() < level {
return nil
}
ctx := ctxPool.Get().(*Context)
ctx.reset(logger, level, prefix)
return ctx
}
func putContext(ctx *Context) {
if ctx.encoder.Cap() < 1024 {
ctxPool.Put(ctx)
}
}
func (ctx *Context) reset(logger *Logger, level Level, prefix string) {
ctx.logger = logger
ctx.level = level
ctx.prefix = prefix
ctx.encoder.reset()
}
// Print prints logging with context ctx. After this call,
// the ctx not available.
func (ctx *Context) Print(msg string) {
if ctx == nil {
return
}
ctx.encoder.finish()
ctx.encoder.writeString(msg)
var (
caller Caller
flags = ctx.logger.GetFlags()
)
if flags&(Lshortfile|Llongfile) != 0 {
_, caller.Filename, caller.Line, _ = runtime.Caller(1)
}
ctx.logger.provider.Print(ctx.level, flags, caller, ctx.prefix, ctx.encoder.String())
putContext(ctx)
}
// Printf prints logging with context ctx by format. After this call,
// the ctx not available.
func (ctx *Context) Printf(msg string, a ...interface{}) {
if ctx == nil {
return
}
ctx.encoder.finish()
fmt.Fprintf(&ctx.encoder, msg, a...)
var (
caller Caller
flags = ctx.logger.GetFlags()
)
if flags&(Lshortfile|Llongfile) != 0 {
_, caller.Filename, caller.Line, _ = runtime.Caller(1)
}
ctx.logger.provider.Print(ctx.level, flags, caller, ctx.prefix, ctx.encoder.String())
putContext(ctx)
}
// Int puts an integer value for key
func (ctx *Context) Int(key string, value int) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeInt(int64(value))
}
return ctx
}
// Int8 puts an 8-bits integer value for key
func (ctx *Context) Int8(key string, value int8) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeInt(int64(value))
}
return ctx
}
// Int16 puts a 16-bits integer value for key
func (ctx *Context) Int16(key string, value int16) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeInt(int64(value))
}
return ctx
}
// Int32 puts a 32-bits integer value for key
func (ctx *Context) Int32(key string, value int32) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeInt(int64(value))
}
return ctx
}
// Int64 puts a 64-bits integer value for key
func (ctx *Context) Int64(key string, value int64) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeInt(value)
}
return ctx
}
// Uint puts an unsigned integer value for key
func (ctx *Context) Uint(key string, value uint) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeUint(uint64(value))
}
return ctx
}
// Uint8 puts an 8-bits unsigned integer value for key
func (ctx *Context) Uint8(key string, value uint8) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeUint(uint64(value))
}
return ctx
}
// Uint16 puts a 16-bits unsigned integer value for key
func (ctx *Context) Uint16(key string, value uint16) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeUint(uint64(value))
}
return ctx
}
// Uint32 puts a 32-bits unsigned integer value for key
func (ctx *Context) Uint32(key string, value uint32) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeUint(uint64(value))
}
return ctx
}
// Uint64 puts a 64-bits unsigned integer value for key
func (ctx *Context) Uint64(key string, value uint64) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeUint(value)
}
return ctx
}
// Float32 puts a 32-bits floating value for key
func (ctx *Context) Float32(key string, value float32) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeFloat32(value)
}
return ctx
}
// Float64 puts a 64-bits floating value for key
func (ctx *Context) Float64(key string, value float64) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeFloat64(value)
}
return ctx
}
// Complex64 puts a 64-bits complex value for key
func (ctx *Context) Complex64(key string, value complex64) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeComplex64(value)
}
return ctx
}
// Complex128 puts a 128-bits complex value for key
func (ctx *Context) Complex128(key string, value complex128) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeComplex128(value)
}
return ctx
}
// Byte puts a byte value for key
func (ctx *Context) Byte(key string, value byte) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeByte(value)
}
return ctx
}
// Rune puts a rune value for key
func (ctx *Context) Rune(key string, value rune) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeRune(value)
}
return ctx
}
// Bool puts a boolean value for key
func (ctx *Context) Bool(key string, value bool) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeBool(value)
}
return ctx
}
// String puts a string value for key
func (ctx *Context) String(key string, value string) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeString(value)
}
return ctx
}
// Error puts an error value for key
func (ctx *Context) Error(key string, value error) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
if value == nil {
ctx.encoder.encodeNil()
} else {
ctx.encoder.buf = strconv.AppendQuote(ctx.encoder.buf, value.Error())
}
}
return ctx
}
// Any puts an any value for key
func (ctx *Context) Any(key string, value interface{}) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
if value == nil {
ctx.encoder.encodeNil()
} else {
switch x := value.(type) {
case error:
ctx.encoder.encodeString(x.Error())
case fmt.Stringer:
ctx.encoder.encodeString(x.String())
case string:
ctx.encoder.encodeString(x)
case appendFormatter:
ctx.encoder.buf = x.AppendFormat(ctx.encoder.buf)
default:
if !ctx.encoder.encodeScalar(value) {
ctx.encoder.encodeString(fmt.Sprintf("%v", value))
}
}
}
}
return ctx
}
// Type puts a type info of value for key
func (ctx *Context) Type(key string, value interface{}) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
if value == nil {
ctx.encoder.encodeString("nil")
} else {
ctx.encoder.encodeString(reflect.TypeOf(value).String())
}
}
return ctx
}
// Exec puts a result value of function for key
func (ctx *Context) Exec(key string, stringer func() string) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.encodeString(stringer())
}
return ctx
}
func (ctx *Context) writeTime(key string, value time.Time, layout string) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
ctx.encoder.buf = append(ctx.encoder.buf, '"')
ctx.encoder.buf = value.AppendFormat(ctx.encoder.buf, layout)
ctx.encoder.buf = append(ctx.encoder.buf, '"')
}
return ctx
}
// Date puts a date for key
func (ctx *Context) Date(key string, value time.Time) *Context {
return ctx.writeTime(key, value, "2006-01-02Z07:00")
}
// Time puts a time for key
func (ctx *Context) Time(key string, value time.Time) *Context {
return ctx.writeTime(key, value, time.RFC3339Nano)
}
// Clock puts a clock for key
func (ctx *Context) Clock(key string, value time.Time) *Context {
return ctx.writeTime(key, value, "15:04:05")
}
// Seconds puts a time accurate to the second for key
func (ctx *Context) Seconds(key string, value time.Time) *Context {
return ctx.writeTime(key, value, time.RFC3339)
}
// Milliseconds puts a time accurate to the millisecond for key
func (ctx *Context) Milliseconds(key string, value time.Time) *Context {
return ctx.writeTime(key, value, "2006-01-02T15:04:05.999Z07:00")
}
// Microseconds puts a time accurate to the microsecond for key
func (ctx *Context) Microseconds(key string, value time.Time) *Context {
return ctx.writeTime(key, value, "2006-01-02T15:04:05.999999Z07:00")
}
// Duration puts a duration value for key
func (ctx *Context) Duration(key string, value time.Duration) *Context {
if ctx != nil {
ctx.encoder.encodeKey(key)
const reserved = 32
l := len(ctx.encoder.buf)
if cap(ctx.encoder.buf)-l < reserved {
ctx.encoder.grow(reserved)
}
n := formatDuration(ctx.encoder.buf[l:l+reserved], value)
ctx.encoder.buf = ctx.encoder.buf[:l+n]
}
return ctx
}