-
Notifications
You must be signed in to change notification settings - Fork 26
/
record.go
468 lines (385 loc) · 10.6 KB
/
record.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package slog
import (
"context"
"fmt"
"runtime"
"strconv"
"time"
"github.com/gookit/goutil/strutil"
)
// Record a log record definition
type Record struct {
logger *Logger
// reuse flag for reuse a record, will not be released on after write.
// so, if you want reuse a record, you must call Reused() method.
// release a record need call Release() method.
reuse bool
// Mark whether the current record is released to the pool. TODO
freed bool
// inited flag for record
inited bool
// Time for record log, if is empty will use now.
//
// TIP: Will be emptied after each use (write)
Time time.Time
// Level log level for record
Level Level
// level name cache from Level
levelName string
// Channel log channel name. eg: "order", "goods", "user"
Channel string
Message string
// Ctx context.Context
Ctx context.Context
// Fields custom fields data.
// Contains all the fields set by the user.
Fields M
// Data log context data
Data M
// Extra log extra data
Extra M
// Caller information
Caller *runtime.Frame
// CallerFlag value. default is equals to Logger.CallerFlag
CallerFlag uint8
// CallerSkip value. default is equals to Logger.CallerSkip
CallerSkip int
// EnableStack enable stack info, default is false. TODO
EnableStack bool
// Buffer Can use Buffer on formatter
// Buffer *bytes.Buffer
// log input args backups, from log() and logf(). its dont use in formatter.
Fmt string
Args []any
}
func newRecord(logger *Logger) *Record {
return &Record{
logger: logger,
Channel: strutil.OrElse(logger.ChannelName, DefaultChannelName),
// with some options
CallerFlag: logger.CallerFlag,
CallerSkip: logger.CallerSkip,
// init map data field
// Data: make(M, 2),
// Extra: make(M, 0),
// Fields: make(M, 0),
}
}
// Reused set record is reused, will not be released on after write.
func (r *Record) Reused() *Record {
r.reuse = true
return r
}
// Release manual release record to pool
func (r *Record) Release() {
if r.reuse {
r.reuse = false
r.logger.releaseRecord(r)
}
}
//
// ---------------------------------------------------------------------------
// Copy record with something
// ---------------------------------------------------------------------------
//
// WithTime set the record time
func (r *Record) WithTime(t time.Time) *Record {
nr := r.Copy()
nr.Time = t
return nr
}
// WithCtx on record
func (r *Record) WithCtx(ctx context.Context) *Record { return r.WithContext(ctx) }
// WithContext on record
func (r *Record) WithContext(ctx context.Context) *Record {
nr := r.Copy()
nr.Ctx = ctx
return nr
}
// WithError on record
func (r *Record) WithError(err error) *Record {
return r.WithFields(M{FieldKeyError: err})
}
// WithData on record
func (r *Record) WithData(data M) *Record {
nr := r.Copy()
nr.Data = data
return nr
}
// WithField with a new field to record
//
// Note: add field need config Formatter template fields.
func (r *Record) WithField(name string, val any) *Record {
return r.WithFields(M{name: val})
}
// WithFields with new fields to record
//
// Note: add field need config Formatter template fields.
func (r *Record) WithFields(fields M) *Record {
nr := r.Copy()
if nr.Fields == nil {
nr.Fields = make(M, len(fields))
}
for k, v := range fields {
nr.Fields[k] = v
}
return nr
}
// Copy new record from old record
func (r *Record) Copy() *Record {
dataCopy := make(M, len(r.Data))
for k, v := range r.Data {
dataCopy[k] = v
}
fieldsCopy := make(M, len(r.Fields))
for k, v := range r.Fields {
fieldsCopy[k] = v
}
extraCopy := make(M, len(r.Extra))
for k, v := range r.Extra {
extraCopy[k] = v
}
return &Record{
// reuse: true, // copy record is reused
logger: r.logger,
Channel: r.Channel,
// Time: r.Time,
Level: r.Level,
levelName: r.levelName,
CallerFlag: r.CallerFlag,
CallerSkip: r.CallerSkip,
Message: r.Message,
Data: dataCopy,
Extra: extraCopy,
Fields: fieldsCopy,
}
}
//
// ---------------------------------------------------------------------------
// Direct set value to record
// ---------------------------------------------------------------------------
//
// SetCtx on record
func (r *Record) SetCtx(ctx context.Context) *Record { return r.SetContext(ctx) }
// SetContext on record
func (r *Record) SetContext(ctx context.Context) *Record {
r.Ctx = ctx
return r
}
// SetData on record
func (r *Record) SetData(data M) *Record {
r.Data = data
return r
}
// AddData on record
func (r *Record) AddData(data M) *Record {
if r.Data == nil {
r.Data = data
return r
}
for k, v := range data {
r.Data[k] = v
}
return r
}
// AddValue add Data value to record
func (r *Record) AddValue(key string, value any) *Record {
if r.Data == nil {
r.Data = make(M, 8)
}
r.Data[key] = value
return r
}
// Value get Data value from record
func (r *Record) Value(key string) any {
if r.Data == nil {
return nil
}
return r.Data[key]
}
// SetExtra information on record
func (r *Record) SetExtra(data M) *Record {
r.Extra = data
return r
}
// AddExtra information on record
func (r *Record) AddExtra(data M) *Record {
if r.Extra == nil {
r.Extra = data
return r
}
for k, v := range data {
r.Extra[k] = v
}
return r
}
// SetExtraValue on record
func (r *Record) SetExtraValue(k string, v any) {
if r.Extra == nil {
r.Extra = make(M, 8)
}
r.Extra[k] = v
}
// SetTime on record
func (r *Record) SetTime(t time.Time) *Record {
r.Time = t
return r
}
// AddField add new field to the record
func (r *Record) AddField(name string, val any) *Record {
if r.Fields == nil {
r.Fields = make(M, 8)
}
r.Fields[name] = val
return r
}
// AddFields add new fields to the record
func (r *Record) AddFields(fields M) *Record {
if r.Fields == nil {
r.Fields = fields
return r
}
for n, v := range fields {
r.Fields[n] = v
}
return r
}
// SetFields to the record
func (r *Record) SetFields(fields M) *Record {
r.Fields = fields
return r
}
// Field value get from record
func (r *Record) Field(key string) any {
if r.Fields == nil {
return nil
}
return r.Fields[key]
}
//
// ---------------------------------------------------------------------------
// Add log message with builder
// TODO r.Build(InfoLevel).Str().Int().Float().Msg()
// ---------------------------------------------------------------------------
//
// Object data on record TODO optimize performance
// func (r *Record) Obj(obj fmt.Stringer) *Record {
// r.Data = ctx
// return r
// }
// Object data on record TODO optimize performance
// func (r *Record) Any(v any) *Record {
// r.Data = ctx
// return r
// }
// func (r *Record) Str(message string) {
// r.logWrite(level, []byte(message))
// }
// func (r *Record) Int(val int) {
// r.logWrite(level, []byte(message))
// }
//
// ---------------------------------------------------------------------------
// Add log message with level
// ---------------------------------------------------------------------------
//
func (r *Record) log(level Level, args []any) {
r.Level = level
if r.logger.BackupArgs {
r.Args = args
}
// r.Message = strutil.Byte2str(formatArgsWithSpaces(args)) // will reduce memory allocation once
r.Message = formatArgsWithSpaces(args)
// do write log, then release record
r.logger.writeRecord(level, r)
r.logger.releaseRecord(r)
}
func (r *Record) logf(level Level, format string, args []any) {
if r.logger.BackupArgs {
r.Fmt, r.Args = format, args
}
r.Level = level
r.Message = fmt.Sprintf(format, args...)
// do write log, then release record
r.logger.writeRecord(level, r)
r.logger.releaseRecord(r)
}
// Log a message with level
func (r *Record) Log(level Level, args ...any) { r.log(level, args) }
// Logf a message with level
func (r *Record) Logf(level Level, format string, args ...any) {
r.logf(level, format, args)
}
// Info logs a message at level Info
func (r *Record) Info(args ...any) { r.log(InfoLevel, args) }
// Infof logs a message at level Info
func (r *Record) Infof(format string, args ...any) {
r.logf(InfoLevel, format, args)
}
// Trace logs a message at level Trace
func (r *Record) Trace(args ...any) { r.log(TraceLevel, args) }
// Tracef logs a message at level Trace
func (r *Record) Tracef(format string, args ...any) {
r.logf(TraceLevel, format, args)
}
// Error logs a message at level Error
func (r *Record) Error(args ...any) { r.log(ErrorLevel, args) }
// Errorf logs a message at level Error
func (r *Record) Errorf(format string, args ...any) {
r.logf(ErrorLevel, format, args)
}
// Warn logs a message at level Warn
func (r *Record) Warn(args ...any) { r.log(WarnLevel, args) }
// Warnf logs a message at level Warn
func (r *Record) Warnf(format string, args ...any) {
r.logf(WarnLevel, format, args)
}
// Notice logs a message at level Notice
func (r *Record) Notice(args ...any) { r.log(NoticeLevel, args) }
// Noticef logs a message at level Notice
func (r *Record) Noticef(format string, args ...any) {
r.logf(NoticeLevel, format, args)
}
// Debug logs a message at level Debug
func (r *Record) Debug(args ...any) { r.log(DebugLevel, args) }
// Debugf logs a message at level Debug
func (r *Record) Debugf(format string, args ...any) {
r.logf(DebugLevel, format, args)
}
// Print logs a message at level Print
func (r *Record) Print(args ...any) { r.log(PrintLevel, args) }
// Println logs a message at level Print, will not append \n. alias of Print
func (r *Record) Println(args ...any) { r.log(PrintLevel, args) }
// Printf logs a message at level Print
func (r *Record) Printf(format string, args ...any) {
r.logf(PrintLevel, format, args)
}
// Fatal logs a message at level Fatal
func (r *Record) Fatal(args ...any) { r.log(FatalLevel, args) }
// Fatalln logs a message at level Fatal
func (r *Record) Fatalln(args ...any) { r.log(FatalLevel, args) }
// Fatalf logs a message at level Fatal
func (r *Record) Fatalf(format string, args ...any) {
r.logf(FatalLevel, format, args)
}
// Panic logs a message at level Panic
func (r *Record) Panic(args ...any) { r.log(PanicLevel, args) }
// Panicln logs a message at level Panic
func (r *Record) Panicln(args ...any) { r.log(PanicLevel, args) }
// Panicf logs a message at level Panic
func (r *Record) Panicf(format string, args ...any) {
r.logf(PanicLevel, format, args)
}
// ---------------------------------------------------------------------------
// helper methods
// ---------------------------------------------------------------------------
// LevelName get
func (r *Record) LevelName() string { return r.levelName }
// GoString of the record
func (r *Record) GoString() string {
return "slog: " + r.Message
}
func (r *Record) timestamp() string {
s := strconv.FormatInt(r.Time.UnixMicro(), 10)
return s[:10] + "." + s[10:]
}