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

Function TestFieldsInNestedDictionary

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

Source from the content-addressed store, hash-verified

163}
164
165func TestFieldsInNestedDictionary(t *testing.T) {
166 formatter := &JSONFormatter{
167 DataKey: "args",
168 }
169
170 logEntry := WithFields(Fields{
171 "level": "level",
172 "test": "test",
173 })
174 logEntry.Level = InfoLevel
175
176 b, err := formatter.Format(logEntry)
177 if err != nil {
178 t.Fatal("Unable to format entry: ", err)
179 }
180
181 entry := make(map[string]interface{})
182 err = json.Unmarshal(b, &entry)
183 if err != nil {
184 t.Fatal("Unable to unmarshal formatted entry: ", err)
185 }
186
187 args := entry["args"].(map[string]interface{})
188
189 for _, field := range []string{"test", "level"} {
190 if value, present := args[field]; !present || value != field {
191 t.Errorf("Expected field %v to be present under 'args'; untouched", field)
192 }
193 }
194
195 for _, field := range []string{"test", "fields.level"} {
196 if _, present := entry[field]; present {
197 t.Errorf("Expected field %v not to be present at top level", field)
198 }
199 }
200
201 // with nested object, "level" shouldn't clash
202 if entry["level"] != "info" {
203 t.Errorf("Expected 'level' field to contain 'info'")
204 }
205}
206
207func TestJSONEntryEndsWithNewline(t *testing.T) {
208 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