Format renders a single log entry. A key named "raw" is discarded here as it's for json consumption.
(entry *log.Entry)
| 81 | |
| 82 | // Format renders a single log entry. A key named "raw" is discarded here as it's for json consumption. |
| 83 | func (f *TextFormatter) Format(entry *log.Entry) ([]byte, error) { |
| 84 | var b *bytes.Buffer |
| 85 | keys := make([]string, 0, len(entry.Data)) |
| 86 | for k := range entry.Data { |
| 87 | // Discard key named "raw" as it's for json consumption only. |
| 88 | if k != "raw" { |
| 89 | keys = append(keys, k) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if !f.DisableSorting { |
| 94 | sort.Strings(keys) |
| 95 | } |
| 96 | if entry.Buffer != nil { |
| 97 | b = entry.Buffer |
| 98 | } else { |
| 99 | b = &bytes.Buffer{} |
| 100 | } |
| 101 | |
| 102 | prefixFieldClashes(entry.Data) |
| 103 | |
| 104 | f.Do(func() { f.init(entry) }) |
| 105 | |
| 106 | isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors |
| 107 | |
| 108 | timestampFormat := f.TimestampFormat |
| 109 | if timestampFormat == "" { |
| 110 | timestampFormat = defaultTimestampFormat |
| 111 | } |
| 112 | if isColored { |
| 113 | f.printColored(b, entry, keys, timestampFormat) |
| 114 | } else { |
| 115 | if !f.DisableTimestamp { |
| 116 | f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) |
| 117 | } |
| 118 | if entry.Message != "" { |
| 119 | f.appendValue(b, entry.Message) |
| 120 | } |
| 121 | for _, key := range keys { |
| 122 | f.appendKeyValue(b, key, entry.Data[key]) |
| 123 | } |
| 124 | } |
| 125 | b.WriteByte('\n') |
| 126 | return b.Bytes(), nil |
| 127 | } |
| 128 | |
| 129 | func (f *TextFormatter) printColored(b *bytes.Buffer, entry *log.Entry, keys []string, _ string) { |
| 130 | var levelColor int |