1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 const nAttrsInline = 5
15 // A Record holds information about a log event.
16 // Copies of a Record share state.
17 // Do not modify a Record after handing out a copy to it.
18 // Use [Record.Clone] to create a copy with no shared state.
20 // The time at which the output method (Log, Info, etc.) was called.
26 // The level of the event.
29 // The program counter at the time the record was constructed, as determined
30 // by runtime.Callers. If zero, no program counter is available.
32 // The only valid use for this value is as an argument to
33 // [runtime.CallersFrames]. In particular, it must not be passed to
34 // [runtime.FuncForPC].
37 // Allocation optimization: an inline array sized to hold
38 // the majority of log calls (based on examination of open-source
39 // code). It holds the start of the list of Attrs.
40 front [nAttrsInline]Attr
42 // The number of Attrs in front.
45 // The list of Attrs except for those in front.
47 // - len(back) > 0 iff nFront == len(front)
48 // - Unused array elements are zero. Used to detect mistakes.
52 // NewRecord creates a Record from the given arguments.
53 // Use [Record.AddAttrs] to add attributes to the Record.
55 // NewRecord is intended for logging APIs that want to support a [Handler] as
57 func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record {
66 // frame returns the runtime.Frame of the log event.
67 // If the Record was created without the necessary information,
68 // or if the location is unavailable, it returns a zero Frame.
69 func (r Record) frame() runtime.Frame {
70 fs := runtime.CallersFrames([]uintptr{r.PC})
75 // Clone returns a copy of the record with no shared state.
76 // The original record and the clone can both be modified
77 // without interfering with each other.
78 func (r Record) Clone() Record {
79 r.back = slices.Clip(r.back) // prevent append from mutating shared array
83 // NumAttrs returns the number of attributes in the Record.
84 func (r Record) NumAttrs() int {
85 return r.nFront + len(r.back)
88 // Attrs calls f on each Attr in the Record.
89 // Iteration stops if f returns false.
90 func (r Record) Attrs(f func(Attr) bool) {
91 for i := 0; i < r.nFront; i++ {
96 for _, a := range r.back {
103 // AddAttrs appends the given Attrs to the Record's list of Attrs.
104 func (r *Record) AddAttrs(attrs ...Attr) {
105 n := copy(r.front[r.nFront:], attrs)
107 // Check if a copy was modified by slicing past the end
108 // and seeing if the Attr there is non-zero.
109 if cap(r.back) > len(r.back) {
110 end := r.back[:len(r.back)+1][len(r.back)]
112 panic("copies of a slog.Record were both modified")
115 r.back = append(r.back, attrs[n:]...)
118 // Add converts the args to Attrs as described in [Logger.Log],
119 // then appends the Attrs to the Record's list of Attrs.
120 func (r *Record) Add(args ...any) {
123 a, args = argsToAttr(args)
124 if r.nFront < len(r.front) {
125 r.front[r.nFront] = a
129 r.back = make([]Attr, 0, countAttrs(args))
131 r.back = append(r.back, a)
137 // countAttrs returns the number of Attrs that would be created from args.
138 func countAttrs(args []any) int {
140 for i := 0; i < len(args); i++ {
142 if _, ok := args[i].(string); ok {
149 const badKey = "!BADKEY"
151 // argsToAttr turns a prefix of the nonempty args slice into an Attr
152 // and returns the unconsumed portion of the slice.
153 // If args[0] is an Attr, it returns it.
154 // If args[0] is a string, it treats the first two elements as
156 // Otherwise, it treats args[0] as a value with a missing key.
157 func argsToAttr(args []any) (Attr, []any) {
158 switch x := args[0].(type) {
161 return String(badKey, x), nil
163 return Any(x, args[1]), args[2:]
169 return Any(badKey, x), args[1:]