Repository navigation
Expand file tree
/
Copy pathrecord.go
More file actions
74 lines (65 loc) · 1.87 KB
/
Copy pathrecord.go
File metadata and controls
74 lines (65 loc) · 1.87 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
package logger
import (
"context"
"sync"
"time"
)
// Record is one log event flowing through the processor pipeline. It is
// pooled: never retain a *Record past the call that produced it. Copy fields
// out (or call Clone) if you must keep them (e.g. async transport, buffering).
type Record struct {
Time time.Time
Level Level
Message string
// PC is the caller program counter (0 if caller capture is disabled);
// resolved lazily so the cost is only paid when actually formatted.
PC uint64
// EventName, when set, is the stable identity of a named typed event
// (mulog "events not messages" + OTEL EventName). Use it instead of
// prose for analytics/AI-friendly logs.
EventName string
// Fields are this event's own attributes plus any inherited via With().
Fields []Field
// Ctx carries request-scoped values + the active trace span. Never nil
// in the pipeline (defaults to context.Background()).
Ctx context.Context
pooled bool
}
var recordPool = sync.Pool{New: func() any { return &Record{} }}
func newRecord() *Record {
r := recordPool.Get().(*Record)
r.pooled = true
return r
}
func (r *Record) reset() {
r.Time = time.Time{}
r.Level = 0
r.Message = ""
r.EventName = ""
r.PC = 0
r.Fields = r.Fields[:0]
r.Ctx = nil
}
func (r *Record) release() {
if !r.pooled {
return
}
r.reset()
recordPool.Put(r)
}
// Clone returns a heap copy safe to retain after the pipeline returns. The
// async transports call this before handing a record to a worker goroutine.
func (r *Record) Clone() *Record {
c := &Record{
Time: r.Time,
Level: r.Level,
Message: r.Message,
EventName: r.EventName,
PC: r.PC,
Ctx: r.Ctx,
}
c.Fields = append(make([]Field, 0, len(r.Fields)), r.Fields...)
return c
}
// AddField appends a field in place (used by enrich processors).
func (r *Record) AddField(f Field) { r.Fields = append(r.Fields, f) }