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

Function prefixFieldClashes

formatter.go:42–78  ·  view source on GitHub ↗

This is to not silently overwrite `time`, `msg`, `func` and `level` fields when dumping it. If this code wasn't there doing: logrus.WithField("level", 1).Info("hello") Would just silently drop the user provided level. Instead with this code it'll logged as: {"level": "info", "fields.level": 1, "m

(data Fields, fieldMap FieldMap, reportCaller bool)

Source from the content-addressed store, hash-verified

40// It's not exported because it's still using Data in an opinionated way. It's to
41// avoid code duplication between the two default formatters.
42func prefixFieldClashes(data Fields, fieldMap FieldMap, reportCaller bool) {
43 timeKey := fieldMap.resolve(FieldKeyTime)
44 if t, ok := data[timeKey]; ok {
45 data["fields."+timeKey] = t
46 delete(data, timeKey)
47 }
48
49 msgKey := fieldMap.resolve(FieldKeyMsg)
50 if m, ok := data[msgKey]; ok {
51 data["fields."+msgKey] = m
52 delete(data, msgKey)
53 }
54
55 levelKey := fieldMap.resolve(FieldKeyLevel)
56 if l, ok := data[levelKey]; ok {
57 data["fields."+levelKey] = l
58 delete(data, levelKey)
59 }
60
61 logrusErrKey := fieldMap.resolve(FieldKeyLogrusError)
62 if l, ok := data[logrusErrKey]; ok {
63 data["fields."+logrusErrKey] = l
64 delete(data, logrusErrKey)
65 }
66
67 // If reportCaller is not set, 'func' will not conflict.
68 if reportCaller {
69 funcKey := fieldMap.resolve(FieldKeyFunc)
70 if l, ok := data[funcKey]; ok {
71 data["fields."+funcKey] = l
72 }
73 fileKey := fieldMap.resolve(FieldKeyFile)
74 if l, ok := data[fileKey]; ok {
75 data["fields."+fileKey] = l
76 }
77 }
78}

Callers 2

FormatMethod · 0.85
FormatMethod · 0.85

Calls 1

resolveMethod · 0.80

Tested by

no test coverage detected