format formats a log record.
(r slog.Record)
| 21 | |
| 22 | // format formats a log record. |
| 23 | func (s *formatterStructured) format(r slog.Record) { |
| 24 | s.prefix = newBuffer() |
| 25 | defer func() { |
| 26 | s.prefix.free() |
| 27 | }() |
| 28 | |
| 29 | // Append timestamp |
| 30 | s.appendKey(slog.TimeKey) |
| 31 | s.appendTime(r.Time) |
| 32 | |
| 33 | // Append log level |
| 34 | s.appendKey(slog.LevelKey) |
| 35 | s.appendString(s.levelName(r.Level), true) |
| 36 | |
| 37 | // Append message |
| 38 | s.appendKey(slog.MessageKey) |
| 39 | s.appendString(r.Message, true) |
| 40 | |
| 41 | // Append groups added with [Handler.WithAttrs] and [Handler.WithGroup] |
| 42 | for _, g := range s.groups { |
| 43 | if g.name != "" { |
| 44 | s.openGroup(g.name) |
| 45 | } |
| 46 | |
| 47 | s.appendAttributes(g.attrs) |
| 48 | } |
| 49 | |
| 50 | // Append attributes from the record |
| 51 | r.Attrs(func(attr slog.Attr) bool { |
| 52 | s.appendAttribute(attr) |
| 53 | return true |
| 54 | }) |
| 55 | |
| 56 | // Append error, source, and stack if present |
| 57 | if s.error.Key != "" { |
| 58 | s.appendKey(s.error.Key) |
| 59 | s.appendValue(s.error.Value) |
| 60 | |
| 61 | if docsURL := s.errorDocsURL(); docsURL != nil { |
| 62 | s.appendKey(docsURL.Key) |
| 63 | s.appendValue(docsURL.Value) |
| 64 | } |
| 65 | } |
| 66 | if s.source.Key != "" { |
| 67 | s.appendKey(s.source.Key) |
| 68 | s.appendValue(s.source.Value) |
| 69 | } |
| 70 | if s.stack.Key != "" { |
| 71 | s.appendKey(s.stack.Key) |
| 72 | s.appendValue(s.stack.Value) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // appendAttributes appends a list of attributes to the buffer. |
| 77 | func (s *formatterStructured) appendAttributes(attrs []slog.Attr) { |
nothing calls this directly
no test coverage detected