| 25 | } |
| 26 | |
| 27 | func TestLogrus(t *testing.T) { |
| 28 | t.Parallel() |
| 29 | |
| 30 | out := new(bytes.Buffer) |
| 31 | logrus := Logrus(out, "v1", 1, 2) |
| 32 | |
| 33 | // Configured verbosity discards. |
| 34 | assert.Assert(t, logrus.Enabled(1)) |
| 35 | assert.Assert(t, logrus.Enabled(2)) |
| 36 | assert.Assert(t, !logrus.Enabled(3)) |
| 37 | |
| 38 | // Default level is INFO. |
| 39 | // Version field is always present. |
| 40 | out.Reset() |
| 41 | logrus.Info(0, "") |
| 42 | assertLogrusContains(t, out.String(), `level=info version=v1`) |
| 43 | |
| 44 | // Configured level or higher is DEBUG. |
| 45 | out.Reset() |
| 46 | logrus.Info(1, "") |
| 47 | assertLogrusContains(t, out.String(), `level=debug`) |
| 48 | out.Reset() |
| 49 | logrus.Info(2, "") |
| 50 | assertLogrusContains(t, out.String(), `level=debug`) |
| 51 | |
| 52 | // Any error is ERROR level. |
| 53 | out.Reset() |
| 54 | logrus.Error(fmt.Errorf("%s", "dang"), "") |
| 55 | assertLogrusContains(t, out.String(), `level=error error=dang`) |
| 56 | |
| 57 | // A wrapped error includes one frame of its stack. |
| 58 | out.Reset() |
| 59 | _, _, baseline, _ := runtime.Caller(0) |
| 60 | logrus.Error(errors.New("dang"), "") |
| 61 | assertLogrusContains(t, out.String(), fmt.Sprintf(`file="internal/logging/logrus_test.go:%d"`, baseline+1)) |
| 62 | assertLogrusContains(t, out.String(), `func=logging.TestLogrus`) |
| 63 | |
| 64 | out.Reset() |
| 65 | logrus.Info(0, "", "k1", "str", "k2", 13, "k3", false) |
| 66 | assertLogrusContains(t, out.String(), `k1=str k2=13 k3=false`) |
| 67 | |
| 68 | out.Reset() |
| 69 | logrus.Info(0, "banana") |
| 70 | assertLogrusContains(t, out.String(), `msg=banana`) |
| 71 | |
| 72 | // Fields don't overwrite builtins. |
| 73 | out.Reset() |
| 74 | logrus.Error(errors.New("dang"), "banana", |
| 75 | "error", "not-err", |
| 76 | "file", "not-file", |
| 77 | "func", "not-func", |
| 78 | "level", "not-lvl", |
| 79 | "msg", "not-msg", |
| 80 | ) |
| 81 | assertLogrusContains(t, out.String(), `level=error msg=banana error=dang`) |
| 82 | assertLogrusContains(t, out.String(), `fields.error=not-err fields.file=not-file fields.func=not-func`) |
| 83 | assertLogrusContains(t, out.String(), `fields.level=not-lvl fields.msg=not-msg`) |
| 84 | } |