MCPcopy
hub / github.com/uber-go/zap / TestSugarFieldsInvalidPairs

Function TestSugarFieldsInvalidPairs

sugar_test.go:270–288  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

268}
269
270func TestSugarFieldsInvalidPairs(t *testing.T) {
271 withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
272 logger.With(42, "foo", []string{"bar"}, "baz").Info("")
273 output := logs.AllUntimed()
274
275 // Double-check that the actual message was logged.
276 require.Equal(t, 2, len(output), "Unexpected number of entries logged.")
277 require.Equal(t, observer.LoggedEntry{Context: []Field{}}, output[1], "Unexpected non-error log entry.")
278
279 // Assert that the error message's structured fields serialize properly.
280 require.Equal(t, 1, len(output[0].Context), "Expected one field in error entry context.")
281 enc := zapcore.NewMapObjectEncoder()
282 output[0].Context[0].AddTo(enc)
283 assert.Equal(t, []interface{}{
284 map[string]interface{}{"position": int64(0), "key": int64(42), "value": "foo"},
285 map[string]interface{}{"position": int64(2), "key": []interface{}{"bar"}, "value": "baz"},
286 }, enc.Fields["invalid"], "Unexpected output when logging invalid key-value pairs.")
287 })
288}
289
290func TestSugarStructuredLogging(t *testing.T) {
291 tests := []struct {

Callers

nothing calls this directly

Calls 6

NewMapObjectEncoderFunction · 0.92
withSugarFunction · 0.85
AllUntimedMethod · 0.80
AddToMethod · 0.80
WithMethod · 0.65
InfoMethod · 0.45

Tested by

no test coverage detected