-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub.zzzgo
418 lines (375 loc) · 14 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
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
log *Log
}
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, 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 string, 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(), Static(), 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 string, 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 string, 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
}
// DefaultSettings are the settings that are used if no setting changes
// are made. Debug 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.TraceLevel
settings.synchronousFlushWhenDone = true
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 (log *Log) Settings() LogSettings {
return log.settings.Copy()
}
// Sub is a first step in creating a sub-Log from the current log.
// Sub allows log settings to be modified. The returned value must
// be used. It is used by a call to sub.Log(), sub.Fork(), or
// sub.Step().
//
// Logs created from Sub() are done when their parent is done.
func (log *Log) Sub() *Sub {
return &Sub{
settings: log.settings.Copy(),
log: log,
}
}
// Detach followed by Fork() or Step() create a sub-span/log that is detached from
// it's parent. A Done() on the parent does not imply Done() on the detached
// log.
func (sub Sub) Detach() *Detaching {
sub.detached = true
return &Detaching{
sub: &sub,
}
}
func (d *Detaching) Step(msg string, mods ...SeedModifier) *Log { return d.sub.Step(msg, mods...) }
func (d *Detaching) Fork(msg string, mods ...SeedModifier) *Log { return d.sub.Fork(msg, mods...) }
// Fork creates a new Log that does not need to be terminated because
// it is assumed to be done with the current log is finished. The new log
// has its own span.
func (sub *Sub) Fork(msg string, mods ...SeedModifier) *Log {
seed := sub.log.capSpan.Seed(mods...)
counter := int(atomic.AddInt32(&sub.log.span.forkCounter, 1))
seed.spanSequenceCode += "." + base26(counter-1)
seed.settings = sub.settings
return sub.log.newChildLog(seed, msg, sub.detached)
}
// Step creates a new log that does not need to be terminated -- it
// represents the continued execution of the current log but doing
// something that is different and should be in a fresh span. The expectation
// is that there is a parent log that is creating various sub-logs using
// Step over and over as it does different things.
func (sub *Sub) Step(msg string, mods ...SeedModifier) *Log {
seed := sub.log.capSpan.Seed(mods...)
counter := int(atomic.AddInt32(&sub.log.span.stepCounter, 1))
seed.spanSequenceCode += "." + strconv.Itoa(counter)
seed.settings = sub.settings
return sub.log.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
}
}
}
// 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 is that no logs are discarded.
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 is that no logs are discarded.
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
}
func (sub *Sub) PrefillText(m string) *Sub {
sub.settings.PrefillText(m)
return sub
}
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 (log *Log) sendPrefill() {
if log.settings.prefillData == nil && log.settings.prefillMsg == "" && !log.settings.tagLinesWithSpanSequence {
log.prefilled = log.span.base.NoPrefill()
return
}
prefilling := log.span.base.StartPrefill()
for _, f := range log.settings.prefillData {
f(prefilling)
}
if log.settings.tagLinesWithSpanSequence {
prefilling.String(xopconst.SpanSequenceCode.Key(), log.span.seed.spanSequenceCode, xopbase.StringDataType)
}
log.prefilled = prefilling.PrefillComplete(log.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 Log() 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 Log() 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 string, 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 string, 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 Log() is called.
// Redaction is not supported.
func (sub *Sub) PrefillAny(k string, 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 Log() is called.
// Redaction is not supported.
func (settings *LogSettings) PrefillAny(k string, v interface{}) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Any(k, 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 Log() is called.
func (sub *Sub) PrefillFloat32(k string, v float32) *Sub {
sub.settings.PrefillFloat32(k, v)
return sub
}
func (settings *LogSettings) PrefillFloat32(k string, 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 Log() is called.
//CONDITIONAL ONLY:String
// Redaction is not supported.
//END CONDITIONAL
func (sub *Sub) PrefillZZZ(k string, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k string, 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 Log() is called.
func (sub *Sub) PrefillZZZ(k string, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k string, 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 Log() is called.
func (sub *Sub) PrefillZZZ(k string, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k string, 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 Log() is called.
func (sub *Sub) PrefillZZZ(k string, v zzz) *Sub {
sub.settings.PrefillZZZ(k, v)
return sub
}
func (settings *LogSettings) PrefillZZZ(k string, 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
}