MCPcopy
hub / github.com/sirupsen/logrus / Format

Method Format

text_formatter.go:133–230  ·  view source on GitHub ↗

Format renders a single log entry

(entry *Entry)

Source from the content-addressed store, hash-verified

131
132// Format renders a single log entry
133func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
134 data := make(Fields)
135 for k, v := range entry.Data {
136 data[k] = v
137 }
138 prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
139 keys := make([]string, 0, len(data))
140 for k := range data {
141 keys = append(keys, k)
142 }
143
144 var funcVal, fileVal string
145
146 fixedKeys := make([]string, 0, 4+len(data))
147 if !f.DisableTimestamp {
148 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
149 }
150 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel))
151 if entry.Message != "" {
152 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg))
153 }
154 if entry.err != "" {
155 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
156 }
157 if entry.HasCaller() {
158 if f.CallerPrettyfier != nil {
159 funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
160 } else {
161 funcVal = entry.Caller.Function
162 fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
163 }
164
165 if funcVal != "" {
166 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc))
167 }
168 if fileVal != "" {
169 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile))
170 }
171 }
172
173 if !f.DisableSorting {
174 if f.SortingFunc == nil {
175 sort.Strings(keys)
176 fixedKeys = append(fixedKeys, keys...)
177 } else {
178 if !f.isColored() {
179 fixedKeys = append(fixedKeys, keys...)
180 f.SortingFunc(fixedKeys)
181 } else {
182 f.SortingFunc(keys)
183 }
184 }
185 } else {
186 fixedKeys = append(fixedKeys, keys...)
187 }
188
189 var b *bytes.Buffer
190 if entry.Buffer != nil {

Callers 9

TestFormattingFunction · 0.95
TestQuotingFunction · 0.95
TestEscapingFunction · 0.95
TestEscaping_InterfaceFunction · 0.95
TestTimestampFormatFunction · 0.95
TestNewlineBehaviorFunction · 0.95
TestCustomSortingFunction · 0.95

Calls 10

isColoredMethod · 0.95
initMethod · 0.95
printColoredMethod · 0.95
appendKeyValueMethod · 0.95
prefixFieldClashesFunction · 0.85
HasCallerMethod · 0.80
resolveMethod · 0.80
BytesMethod · 0.80
FormatMethod · 0.65
StringMethod · 0.45

Tested by 9

TestFormattingFunction · 0.76
TestQuotingFunction · 0.76
TestEscapingFunction · 0.76
TestEscaping_InterfaceFunction · 0.76
TestTimestampFormatFunction · 0.76
TestNewlineBehaviorFunction · 0.76
TestCustomSortingFunction · 0.76