format formats a log record.
(r slog.Record)
| 45 | |
| 46 | // format formats a log record. |
| 47 | func (s *formatterJSON) format(r slog.Record) { |
| 48 | // Open the JSON object and defer closing it. |
| 49 | s.buf.append(jsonGroupOpenToken) |
| 50 | defer func() { |
| 51 | s.buf.append(jsonGroupCloseToken) |
| 52 | }() |
| 53 | |
| 54 | // Append timestamp |
| 55 | s.appendKey(slog.TimeKey) |
| 56 | s.appendTime(r.Time) |
| 57 | |
| 58 | // Append log level |
| 59 | s.appendKey(s.levelKey) |
| 60 | s.appendString(s.levelName(r.Level)) |
| 61 | |
| 62 | // Append message |
| 63 | s.appendKey(s.messageKey) |
| 64 | s.appendString(r.Message) |
| 65 | |
| 66 | // Append groups added with [Handler.WithAttrs] and [Handler.WithGroup] |
| 67 | for _, g := range s.groups { |
| 68 | if g.name != "" { |
| 69 | s.openGroup(g.name) |
| 70 | } |
| 71 | |
| 72 | s.appendAttributes(g.attrs) |
| 73 | } |
| 74 | |
| 75 | // Append attributes from the record |
| 76 | r.Attrs(func(attr slog.Attr) bool { |
| 77 | s.appendAttribute(attr) |
| 78 | return true |
| 79 | }) |
| 80 | |
| 81 | // Close all opened groups. |
| 82 | for s.groupsOpened > 0 { |
| 83 | s.closeGroup() |
| 84 | } |
| 85 | |
| 86 | // Append error, source, and stack if present |
| 87 | if s.error.Key != "" { |
| 88 | s.appendKey(s.error.Key) |
| 89 | s.appendValue(s.error.Value) |
| 90 | |
| 91 | if docsURL := s.errorDocsURL(); docsURL != nil { |
| 92 | s.appendKey(docsURL.Key) |
| 93 | s.appendValue(docsURL.Value) |
| 94 | } |
| 95 | } |
| 96 | if s.source.Key != "" { |
| 97 | s.appendKey(s.source.Key) |
| 98 | s.appendValue(s.source.Value) |
| 99 | } |
| 100 | if s.stack.Key != "" { |
| 101 | s.appendKey(s.stack.Key) |
| 102 | s.appendValue(s.stack.Value) |
| 103 | } |
| 104 | } |
nothing calls this directly
no test coverage detected