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

Function TestFieldClashWithRemappedFields

json_formatter_test.go:111–163  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

109}
110
111func TestFieldClashWithRemappedFields(t *testing.T) {
112 formatter := &JSONFormatter{
113 FieldMap: FieldMap{
114 FieldKeyTime: "@timestamp",
115 FieldKeyLevel: "@level",
116 FieldKeyMsg: "@message",
117 },
118 }
119
120 b, err := formatter.Format(WithFields(Fields{
121 "@timestamp": "@timestamp",
122 "@level": "@level",
123 "@message": "@message",
124 "timestamp": "timestamp",
125 "level": "level",
126 "msg": "msg",
127 }))
128 if err != nil {
129 t.Fatal("Unable to format entry: ", err)
130 }
131
132 entry := make(map[string]interface{})
133 err = json.Unmarshal(b, &entry)
134 if err != nil {
135 t.Fatal("Unable to unmarshal formatted entry: ", err)
136 }
137
138 for _, field := range []string{"timestamp", "level", "msg"} {
139 if entry[field] != field {
140 t.Errorf("Expected field %v to be untouched; got %v", field, entry[field])
141 }
142
143 remappedKey := fmt.Sprintf("fields.%s", field)
144 if remapped, ok := entry[remappedKey]; ok {
145 t.Errorf("Expected %s to be empty; got %v", remappedKey, remapped)
146 }
147 }
148
149 for _, field := range []string{"@timestamp", "@level", "@message"} {
150 if entry[field] == field {
151 t.Errorf("Expected field %v to be mapped to an Entry value", field)
152 }
153
154 remappedKey := fmt.Sprintf("fields.%s", field)
155 if remapped, ok := entry[remappedKey]; ok {
156 if remapped != field {
157 t.Errorf("Expected field %v to be copied to %s; got %v", field, remappedKey, remapped)
158 }
159 } else {
160 t.Errorf("Expected field %v to be copied to %s; was absent", field, remappedKey)
161 }
162 }
163}
164
165func TestFieldsInNestedDictionary(t *testing.T) {
166 formatter := &JSONFormatter{

Callers

nothing calls this directly

Calls 4

FormatMethod · 0.95
WithFieldsFunction · 0.85
FatalMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected