-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.zzzgo
470 lines (423 loc) · 16.1 KB
/
sub.zzzgo
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
469
470
// TEMPLATE-FILE
// TEMPLATE-FILE
// TEMPLATE-FILE
package xop
import (
"strconv"
"sync/atomic"
"github.com/xoplog/xop-go/xopat"
"github.com/xoplog/xop-go/xopbase"
"github.com/xoplog/xop-go/xopconst"
"github.com/xoplog/xop-go/xopnum"
)
// Sub holds an ephemeral state of a log being tranformed to a new log.
type Sub struct {
detached bool
settings LogSettings
logger *Logger
}
// Detaching is a ephemeral type used in the chain
//
// child := logger.Sub().Detach().Fork()
// child := logger.Sub().Detach().Step()
//
// to indicate that the new logger/span has an independent lifetime
// from it's parent so a call to Done() on the parent does not imply
// the child is done.
type Detaching struct {
sub *Sub
}
// RedactAnyFunc is used to redact models as they're being logged.
// It is RedactAnyFunc's responsibility to call
//
// baseLine.Any(k, xopbase.ModelArg{
// Model: v,
// })
//
// if it wants the value to be logged. If it does make that call, it
// must pass an immutable value. Perhaps use "github.com/mohae/deepcopy"
// to make a copy?
//
// The provided xopbase.Line may not be retained beyond the duration of
// the function call.
type RedactAnyFunc func(baseLine xopbase.Line, k xopat.K, v interface{}, alreadyImmutable bool)
// RedactStringFunc is used to redact strings as they're being logged.
// It is RedactStringFunc's responsiblity to call
//
// baseLine.String(k, v, xopbase.StringDataType)
//
// if it wants the value to be logged.
//
// RedactStringFunc is applied only to String(), and Stringer() attributes.
// It is not applied to Msg(), Msgf(), or Msgs().
//
// The provided xopbase.Line may not be retained beyond the duration of
// the function call.
type RedactStringFunc func(baseLine xopbase.Line, k xopat.K, v string)
// RedactErrorFunc is used to redact or format errors as they're being
// logged. It is RedactErrorFunc's responsibility to call
//
// baseLine.String(k, v.Error(), xopbase.ErrorDataType)
//
// if it wants the value to be logged. Alternatively, it could log the
// error as a model:
//
// baseLine.Any(k, v)
//
// The provided xopbase.Line may not be retained beyond the duration of
// the function call.
type RedactErrorFunc func(baseLine xopbase.Line, k xopat.K, v error)
type LogSettings struct {
prefillMsg string
prefillData []func(xopbase.Prefilling)
minimumLogLevel xopnum.Level
stackFramesWanted [xopnum.AlertLevel + 1]int // indexed
tagLinesWithSpanSequence bool
synchronousFlushWhenDone bool
redactAny RedactAnyFunc
redactString RedactStringFunc
redactError RedactErrorFunc
stackFilenameRewrite func(string) string
}
// String is for debugging purposes. It is not complete or preformant.
func (settings LogSettings) String() string {
var str string
if settings.prefillMsg != "" {
str += " prefill:" + settings.prefillMsg
}
if len(settings.prefillData) > 0 {
str += " prefillDataCount:" + strconv.Itoa(len(settings.prefillData))
}
if settings.minimumLogLevel != 0 {
str += " minLevel:" + settings.minimumLogLevel.String()
}
if settings.synchronousFlushWhenDone {
str += " flush-when-done"
}
return str
}
// DefaultSettings are the settings that are used if no setting changes
// are made. Trace logs are excluded. Alert and Error level log lines
// get stack traces.
var DefaultSettings = func() LogSettings {
var settings LogSettings
settings.stackFramesWanted[xopnum.AlertLevel] = 20
settings.stackFramesWanted[xopnum.ErrorLevel] = 10
settings.minimumLogLevel = xopnum.DebugLevel
settings.synchronousFlushWhenDone = true
settings.stackFilenameRewrite = func(s string) string { return s }
return settings
}()
func (settings LogSettings) Copy() LogSettings {
if settings.prefillData != nil {
n := make([]func(xopbase.Prefilling), len(settings.prefillData))
copy(n, settings.prefillData)
settings.prefillData = n
}
return settings
}
func (logger *Logger) Settings() LogSettings {
return logger.settings.Copy()
}
// Sub is a first step in creating a sub-Log from the current logger.
// Sub allows log settings to be modified. The returned value must
// be used. It is used by a call to sub.Logger(), sub.Fork(), or
// sub.Step().
//
// Logs created from Sub() are done when their parent is done.
func (logger *Logger) Sub() *Sub {
return &Sub{
settings: logger.settings.Copy(),
logger: logger,
}
}
// Detach followed by Fork() or Step() creates a sub-span/logger that is detached from
// it's parent. A Done() on the parent does not imply Done() on the detached
// logger.
func (sub Sub) Detach() *Detaching {
sub.detached = true
return &Detaching{
sub: &sub,
}
}
func (d *Detaching) Step(msg string, mods ...SeedModifier) *Logger { return d.sub.Step(msg, mods...) }
func (d *Detaching) Fork(msg string, mods ...SeedModifier) *Logger { return d.sub.Fork(msg, mods...) }
// Fork creates a new logger that does not need to be terminated because
// it is assumed to be done with the current logger is finished. The new logger
// has its own span.
func (sub *Sub) Fork(msg string, mods ...SeedModifier) *Logger {
seed := sub.logger.capSpan.SubSeed(mods...)
counter := int(atomic.AddInt32(&sub.logger.span.forkCounter, 1))
seed.spanSequenceCode += "." + base26(counter-1)
seed.settings = sub.settings
return sub.logger.newChildLog(seed, msg, sub.detached)
}
// Step creates a new logger that does not need to be terminated -- it
// represents the continued execution of the current logger but doing
// something that is different and should be in a fresh span. The expectation
// is that there is a parent logger that is creating various sub-logs using
// Step over and over as it does different things.
func (sub *Sub) Step(msg string, mods ...SeedModifier) *Logger {
seed := sub.logger.capSpan.SubSeed(mods...)
counter := int(atomic.AddInt32(&sub.logger.span.stepCounter, 1))
seed.spanSequenceCode += "." + strconv.Itoa(counter)
seed.settings = sub.settings
return sub.logger.newChildLog(seed, msg, sub.detached)
}
// StackFrames sets the number of stack frames to include at
// a logging level. Levels above the given level will be set to
// get least this many. Levels below the given level will be set
// to receive at most this many.
func (sub *Sub) StackFrames(level xopnum.Level, count int) *Sub {
sub.settings.StackFrames(level, count)
return sub
}
// StackFrames sets the number of stack frames to include at
// a logging level. Levels above the given level will be set to
// get least this many. Levels below the given level will be set
// to receive at most this many.
func (settings *LogSettings) StackFrames(level xopnum.Level, frameCount int) {
for _, l := range xopnum.LevelValues() {
current := settings.stackFramesWanted[l]
if l <= level && current > frameCount {
settings.stackFramesWanted[l] = frameCount
}
if l >= level && current < frameCount {
settings.stackFramesWanted[l] = frameCount
}
}
}
// StackFilenameRewrite is used to rewrite filenames in stack
// traces. Generally this is used to eliminate path prefixes.
// An empty return value indicates that the rest of the stack
// trace should be discarded.
func (sub *Sub) StackFilenameRewrite(f func(string) string) *Sub {
sub.settings.StackFilenameRewrite(f)
return sub
}
// StackFilenameRewrite is used to rewrite filenames in stack
// traces. Generally this is used to eliminate path prefixes.
// An empty return value indicates that the rest of the stack
// trace should be discarded.
func (settings *LogSettings) StackFilenameRewrite(f func(string) string) {
settings.stackFilenameRewrite = f
}
// SynchronousFlush sets the behavior for any Flush()
// triggered by a call to Done(). When true, the
// call to Done() will not return until the Flush() is
// complete.
func (sub *Sub) SynchronousFlush(b bool) *Sub {
sub.settings.SynchronousFlush(b)
return sub
}
// SynchronousFlush sets the behavior for any Flush()
// triggered by a call to Done(). When true, the
// call to Done() will not return until the Flush() is
// complete.
func (settings *LogSettings) SynchronousFlush(b bool) {
settings.synchronousFlushWhenDone = b
}
// MinLevel sets the minimum logging level below which logs will
// be discarded. The default minimum level comes from DefaultSettings.
func (sub *Sub) MinLevel(level xopnum.Level) *Sub {
sub.settings.MinLevel(level)
return sub
}
// MinLevel sets the minimum logging level below which logs will
// be discarded. The default minimum level comes from DefaultSettings.
func (settings *LogSettings) MinLevel(level xopnum.Level) {
settings.minimumLogLevel = level
}
func (settings LogSettings) GetMinLevel() xopnum.Level {
return settings.minimumLogLevel
}
// TagLinesWithSpanSequence controls if the span sequence
// indicator (see Fork() and Step()) should be included in
// the prefill data on each line.
func (sub *Sub) TagLinesWithSpanSequence(b bool) *Sub {
sub.settings.TagLinesWithSpanSequence(b)
return sub
}
// TagLinesWithSpanSequence controls if the span sequence
// indicator (see Fork() and Step()) should be included in
// the prefill data on each line.
func (settings *LogSettings) TagLinesWithSpanSequence(b bool) {
settings.tagLinesWithSpanSequence = b
}
// PrefillText is prepended to any eventual Line.Msg() or Line.Template().
// PrefillText will be ignored for Line.Model() and Line.Link().
func (sub *Sub) PrefillText(m string) *Sub {
sub.settings.PrefillText(m)
return sub
}
// PrefillText is prepended to any eventual Line.Msg() or Line.Template()
// PrefillText will be ignored for Line.Model() and Line.Link().
func (settings *LogSettings) PrefillText(m string) {
settings.prefillMsg = m
}
func (sub *Sub) NoPrefill() *Sub {
sub.settings.NoPrefill()
return sub
}
func (settings *LogSettings) NoPrefill() {
settings.prefillData = nil
settings.prefillMsg = ""
}
func (logger *Logger) sendPrefill() {
if logger.settings.prefillData == nil && logger.settings.prefillMsg == "" && !logger.settings.tagLinesWithSpanSequence {
logger.prefilled = logger.span.base.NoPrefill()
return
}
prefilling := logger.span.base.StartPrefill()
for _, f := range logger.settings.prefillData {
f(prefilling)
}
if logger.settings.tagLinesWithSpanSequence {
prefilling.String(xopconst.SpanSequenceCode.Key(), logger.span.seed.spanSequenceCode, xopbase.StringDataType)
}
logger.prefilled = prefilling.PrefillComplete(logger.settings.prefillMsg)
}
// PrefillEmbeddedEnum is used to set a data element that is included on every log
// line.
// PrefillEmbeddedEnum is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillEmbeddedEnum(k xopat.EmbeddedEnum) *Sub {
sub.settings.PrefillEmbeddedEnum(k)
return sub
}
func (settings *LogSettings) PrefillEmbeddedEnum(k xopat.EmbeddedEnum) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Enum(k.EnumAttribute(), k)
})
}
// PrefillEnum is used to set a data element that is included on every log
// line.
// PrefillEnum is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillEnum(k *xopat.EnumAttribute, v xopat.Enum) *Sub {
sub.settings.PrefillEnum(k, v)
return sub
}
func (settings *LogSettings) PrefillEnum(k *xopat.EnumAttribute, v xopat.Enum) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Enum(k, v)
})
}
// PrefillError is used to set a data element that is included on every log
// line. Errors will always be formatted with v.Error(). Redaction is
// not supported.
func (sub *Sub) PrefillError(k xopat.K, v error) *Sub {
sub.settings.PrefillError(k, v)
return sub
}
// PrefillError is used to set a data element that is included on every log
// line. Errors will always be formatted with v.Error(). Redaction is
// not supported.
func (settings *LogSettings) PrefillError(k xopat.K, v error) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.String(k, v.Error(), xopbase.ErrorDataType)
})
}
// PrefillAny is used to set a data element that is included on every log
// line. Values provided with PrefillAny will be copied
// using https://github.com/mohae/deepcopy 's Copy().
// PrefillAny is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
// Redaction is not supported.
func (sub *Sub) PrefillAny(k xopat.K, v interface{}) *Sub {
sub.settings.PrefillAny(k, v)
return sub
}
// PrefillAny is used to set a data element that is included on every log
// line. Values provided with PrefillAny will be copied
// using https://github.com/mohae/deepcopy 's Copy().
// PrefillAny is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
// Redaction is not supported.
func (settings *LogSettings) PrefillAny(k xopat.K, v interface{}) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Any(k, xopbase.ModelArg{Model: v})
})
}
// PrefillFloat32 is used to set a data element that is included on every log
// line.
// PrefillFloat32 is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillFloat32(k xopat.K, v float32) *Sub {
sub.settings.PrefillFloat32(k, v)
return sub
}
func (settings *LogSettings) PrefillFloat32(k xopat.K, v float32) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Float64(k, float64(v), xopbase.Float32DataType)
})
}
// MACRO BaseDataWithoutType SKIP:Any
// PrefillZZZ is used to set a data element that is included on every log
// line.
// PrefillZZZ is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillZZZ(k xopat.K, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k xopat.K, v zzz) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.ZZZ(k, v)
})
}
// MACRO BaseDataWithType
// PrefillZZZ is used to set a data element that is included on every log
// line.
// PrefillZZZ is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillZZZ(k xopat.K, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k xopat.K, v zzz) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.ZZZ(k, v, xopbase.ZZZDataType)
})
}
// MACRO Ints SKIP:Int64
// PrefillZZZ is used to set a data element that is included on every log
// line.
// PrefillZZZ is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillZZZ(k xopat.K, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k xopat.K, v zzz) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Int64(k, int64(v), xopbase.ZZZDataType)
})
}
// MACRO Uints SKIP:Uint64
// PrefillZZZ is used to set a data element that is included on every log
// line.
// PrefillZZZ is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Logger() is called.
func (sub *Sub) PrefillZZZ(k xopat.K, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k xopat.K, v zzz) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Uint64(k, uint64(v), xopbase.ZZZDataType)
})
}
// MACRO AllData ONLY:String,Any,Error
// SetRedactZZZFunc sets a redaction function to be used
// when Line.ZZZ() is called.
func (sub *Sub) SetRedactZZZFunc(f RedactZZZFunc) *Sub {
sub.settings.SetRedactZZZFunc(f)
return sub
}
// MACRO AllData ONLY:String,Any,Error
// SetRedactZZZFunc sets a redaction function to be used
// when Line.ZZZ() is called.
func (settings *LogSettings) SetRedactZZZFunc(f RedactZZZFunc) {
settings.redactZZZ = f
}