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

Function TestNestedLoggingReportsCorrectCaller

logrus_test.go:362–418  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

360}
361
362func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
363 var buffer bytes.Buffer
364 var fields Fields
365
366 logger := New()
367 logger.Out = &buffer
368 logger.Formatter = new(JSONFormatter)
369 logger.ReportCaller = true
370
371 llog := logger.WithField("context", "eating raw fish")
372
373 llog.Info("looks delicious")
374 _, _, line, _ := runtime.Caller(0)
375
376 err := json.Unmarshal(buffer.Bytes(), &fields)
377 require.NoError(t, err, "should have decoded first message")
378 assert.Equal(t, 6, len(fields), "should have msg/time/level/func/context fields")
379 assert.Equal(t, "looks delicious", fields["msg"])
380 assert.Equal(t, "eating raw fish", fields["context"])
381 assert.Equal(t,
382 "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
383 cwd, err := os.Getwd()
384 require.NoError(t, err)
385 assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))
386
387 buffer.Reset()
388
389 logger.WithFields(Fields{
390 "Clyde": "Stubblefield",
391 }).WithFields(Fields{
392 "Jab'o": "Starks",
393 }).WithFields(Fields{
394 "uri": "https://www.youtube.com/watch?v=V5DTznu-9v0",
395 }).WithFields(Fields{
396 "func": "y drummer",
397 }).WithFields(Fields{
398 "James": "Brown",
399 }).Print("The hardest workin' man in show business")
400 _, _, line, _ = runtime.Caller(0)
401
402 err = json.Unmarshal(buffer.Bytes(), &fields)
403 require.NoError(t, err, "should have decoded second message")
404 assert.Equal(t, 11, len(fields), "should have all builtin fields plus foo,bar,baz,...")
405 assert.Equal(t, "Stubblefield", fields["Clyde"])
406 assert.Equal(t, "Starks", fields["Jab'o"])
407 assert.Equal(t, "https://www.youtube.com/watch?v=V5DTznu-9v0", fields["uri"])
408 assert.Equal(t, "y drummer", fields["fields.func"])
409 assert.Equal(t, "Brown", fields["James"])
410 assert.Equal(t, "The hardest workin' man in show business", fields["msg"])
411 assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
412 assert.Equal(t,
413 "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
414 require.NoError(t, err)
415 assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))
416
417 logger.ReportCaller = false // return to default value
418}
419

Callers

nothing calls this directly

Calls 7

NewFunction · 0.85
BytesMethod · 0.80
ResetMethod · 0.80
WithFieldMethod · 0.65
InfoMethod · 0.65
PrintMethod · 0.65
WithFieldsMethod · 0.65

Tested by

no test coverage detected