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

Method Format

json_formatter.go:63–128  ·  view source on GitHub ↗

Format renders a single log entry

(entry *Entry)

Source from the content-addressed store, hash-verified

61
62// Format renders a single log entry
63func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
64 data := make(Fields, len(entry.Data)+4)
65 for k, v := range entry.Data {
66 switch v := v.(type) {
67 case error:
68 // Otherwise errors are ignored by `encoding/json`
69 // https://github.com/sirupsen/logrus/issues/137
70 data[k] = v.Error()
71 default:
72 data[k] = v
73 }
74 }
75
76 if f.DataKey != "" {
77 newData := make(Fields, 4)
78 newData[f.DataKey] = data
79 data = newData
80 }
81
82 prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
83
84 timestampFormat := f.TimestampFormat
85 if timestampFormat == "" {
86 timestampFormat = defaultTimestampFormat
87 }
88
89 if entry.err != "" {
90 data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
91 }
92 if !f.DisableTimestamp {
93 data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
94 }
95 data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
96 data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
97 if entry.HasCaller() {
98 funcVal := entry.Caller.Function
99 fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
100 if f.CallerPrettyfier != nil {
101 funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
102 }
103 if funcVal != "" {
104 data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal
105 }
106 if fileVal != "" {
107 data[f.FieldMap.resolve(FieldKeyFile)] = fileVal
108 }
109 }
110
111 var b *bytes.Buffer
112 if entry.Buffer != nil {
113 b = entry.Buffer
114 } else {
115 b = &bytes.Buffer{}
116 }
117
118 encoder := json.NewEncoder(b)
119 encoder.SetEscapeHTML(!f.DisableHTMLEscape)
120 if f.PrettyPrint {

Callers 15

TestErrorNotLostFunction · 0.95
TestFieldClashWithTimeFunction · 0.95
TestFieldClashWithMsgFunction · 0.95
TestFieldClashWithLevelFunction · 0.95
TestJSONMessageKeyFunction · 0.95
TestJSONLevelKeyFunction · 0.95
TestJSONTimeKeyFunction · 0.95

Calls 8

prefixFieldClashesFunction · 0.85
HasCallerMethod · 0.80
resolveMethod · 0.80
BytesMethod · 0.80
ErrorMethod · 0.65
FormatMethod · 0.65
ErrorfMethod · 0.65
StringMethod · 0.45

Tested by 15

TestErrorNotLostFunction · 0.76
TestFieldClashWithTimeFunction · 0.76
TestFieldClashWithMsgFunction · 0.76
TestFieldClashWithLevelFunction · 0.76
TestJSONMessageKeyFunction · 0.76
TestJSONLevelKeyFunction · 0.76
TestJSONTimeKeyFunction · 0.76