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

Function TestReportCallerWhenConfigured

logrus_test.go:26–67  ·  view source on GitHub ↗

TestReportCaller verifies that when ReportCaller is set, the 'func' field is added, and when it is unset it is not set or modified Verify that functions within the Logrus package aren't considered when discovering the caller.

(t *testing.T)

Source from the content-addressed store, hash-verified

24// Verify that functions within the Logrus package aren't considered when
25// discovering the caller.
26func TestReportCallerWhenConfigured(t *testing.T) {
27 LogAndAssertJSON(t, func(log *Logger) {
28 log.ReportCaller = false
29 log.Print("testNoCaller")
30 }, func(fields Fields) {
31 assert.Equal(t, "testNoCaller", fields["msg"])
32 assert.Equal(t, "info", fields["level"])
33 assert.Nil(t, fields["func"])
34 })
35
36 LogAndAssertJSON(t, func(log *Logger) {
37 log.ReportCaller = true
38 log.Print("testWithCaller")
39 }, func(fields Fields) {
40 assert.Equal(t, "testWithCaller", fields["msg"])
41 assert.Equal(t, "info", fields["level"])
42 assert.Equal(t,
43 "github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields[FieldKeyFunc])
44 })
45
46 LogAndAssertJSON(t, func(log *Logger) {
47 log.ReportCaller = true
48 log.Formatter.(*JSONFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) {
49 return "somekindoffunc", "thisisafilename"
50 }
51 log.Print("testWithCallerPrettyfier")
52 }, func(fields Fields) {
53 assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc])
54 assert.Equal(t, "thisisafilename", fields[FieldKeyFile])
55 })
56
57 LogAndAssertText(t, func(log *Logger) {
58 log.ReportCaller = true
59 log.Formatter.(*TextFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) {
60 return "somekindoffunc", "thisisafilename"
61 }
62 log.Print("testWithCallerPrettyfier")
63 }, func(fields map[string]string) {
64 assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc])
65 assert.Equal(t, "thisisafilename", fields[FieldKeyFile])
66 })
67}
68
69func logSomething(t *testing.T, message string) Fields {
70 var buffer bytes.Buffer

Callers

nothing calls this directly

Calls 3

LogAndAssertJSONFunction · 0.85
LogAndAssertTextFunction · 0.85
PrintMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…