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 // Clone returns a copy of the record with no shared state.
67 // The original record and the clone can both be modified
68 // without interfering with each other.
69 func (r Record) Clone() Record {
70 r.back = slices.Clip(r.back) // prevent append from mutating shared array
74 // NumAttrs returns the number of attributes in the Record.
75 func (r Record) NumAttrs() int {
76 return r.nFront + len(r.back)
79 // Attrs calls f on each Attr in the Record.
80 // Iteration stops if f returns false.
81 func (r Record) Attrs(f func(Attr) bool) {
82 for i := 0; i < r.nFront; i++ {
87 for _, a := range r.back {
94 // AddAttrs appends the given Attrs to the Record's list of Attrs.
95 func (r *Record) AddAttrs(attrs ...Attr) {
96 n := copy(r.front[r.nFront:], attrs)
98 // Check if a copy was modified by slicing past the end
99 // and seeing if the Attr there is non-zero.
100 if cap(r.back) > len(r.back) {
101 end := r.back[:len(r.back)+1][len(r.back)]
103 panic("copies of a slog.Record were both modified")
106 r.back = append(r.back, attrs[n:]...)
109 // Add converts the args to Attrs as described in [Logger.Log],
110 // then appends the Attrs to the Record's list of Attrs.
111 func (r *Record) Add(args ...any) {
114 a, args = argsToAttr(args)
115 if r.nFront < len(r.front) {
116 r.front[r.nFront] = a
120 r.back = make([]Attr, 0, countAttrs(args))
122 r.back = append(r.back, a)
128 // countAttrs returns the number of Attrs that would be created from args.
129 func countAttrs(args []any) int {
131 for i := 0; i < len(args); i++ {
133 if _, ok := args[i].(string); ok {
140 const badKey = "!BADKEY"
142 // argsToAttr turns a prefix of the nonempty args slice into an Attr
143 // and returns the unconsumed portion of the slice.
144 // If args[0] is an Attr, it returns it.
145 // If args[0] is a string, it treats the first two elements as
147 // Otherwise, it treats args[0] as a value with a missing key.
148 func argsToAttr(args []any) (Attr, []any) {
149 switch x := args[0].(type) {
152 return String(badKey, x), nil
154 return Any(x, args[1]), args[2:]
160 return Any(badKey, x), args[1:]
164 // Source describes the location of a line of source code.
166 // Function is the package path-qualified function name containing the
167 // source line. If non-empty, this string uniquely identifies a single
168 // function in the program. This may be the empty string if not known.
169 Function string `json:"function"`
170 // File and Line are the file name and line number (1-based) of the source
171 // line. These may be the empty string and zero, respectively, if not known.
172 File string `json:"file"`
173 Line int `json:"line"`
176 // attrs returns the non-zero fields of s as a slice of attrs.
177 // It is similar to a LogValue method, but we don't want Source
178 // to implement LogValuer because it would be resolved before
179 // the ReplaceAttr function was called.
180 func (s *Source) group() Value {
182 if s.Function != "" {
183 as = append(as, String("function", s.Function))
186 as = append(as, String("file", s.File))
189 as = append(as, Int("line", s.Line))
191 return GroupValue(as...)
194 // source returns a Source for the log event.
195 // If the Record was created without the necessary information,
196 // or if the location is unavailable, it returns a non-nil *Source
198 func (r Record) source() *Source {
199 fs := runtime.CallersFrames([]uintptr{r.PC})
202 Function: f.Function,