cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/log/handler_test.go
134lines · modecode
| 1 | package log |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "log/slog" |
| 9 | "strconv" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/stretchr/testify/require" |
| 14 | ) |
| 15 | |
| 16 | func TestHandler(t *testing.T) { |
| 17 | type testCaseT struct { |
| 18 | run func(l *slog.Logger) |
| 19 | expected string |
| 20 | noColor bool |
| 21 | } |
| 22 | |
| 23 | testCases := []testCaseT{ |
| 24 | { |
| 25 | noColor: true, |
| 26 | run: func(l *slog.Logger) { |
| 27 | l.Debug("foo", slog.Int("count", 5)) |
| 28 | }, |
| 29 | expected: "level=DEBUG msg=foo count=5\n", |
| 30 | }, |
| 31 | { |
| 32 | noColor: false, |
| 33 | run: func(l *slog.Logger) { |
| 34 | l.Debug("foo", slog.Int("count", 5)) |
| 35 | }, |
| 36 | expected: "[2mlevel=[0m[95mDEBUG[0m [2mmsg=[0m[97mfoo[0m [2mcount=[0m[94m5[0m\n", // nolint: staticcheck |
| 37 | }, |
| 38 | { |
| 39 | noColor: true, |
| 40 | run: func(l *slog.Logger) { |
| 41 | l.Debug("foo", slog.Int("count", 5)) |
| 42 | l.Info("bar", slog.String("string", "a b c"), slog.Any("list", []int{1, 2, 3})) |
| 43 | }, |
| 44 | expected: "level=DEBUG msg=foo count=5\nlevel=INFO msg=bar string=\"a b c\" list=[1,2,3]\n", |
| 45 | }, |
| 46 | { |
| 47 | noColor: true, |
| 48 | run: func(l *slog.Logger) { |
| 49 | l.Warn("bar", slog.Any("strings", []string{"a", "b", "c + d"})) |
| 50 | }, |
| 51 | expected: "level=WARN msg=bar strings=[\"a\",\"b\",\"c + d\"]\n", |
| 52 | }, |
| 53 | { |
| 54 | noColor: true, |
| 55 | run: func(l *slog.Logger) { |
| 56 | l.Error("bar", slog.Any("err", errors.New("error"))) |
| 57 | }, |
| 58 | expected: "level=ERROR msg=bar err=error\n", |
| 59 | }, |
| 60 | { |
| 61 | noColor: false, |
| 62 | run: func(l *slog.Logger) { |
| 63 | l.Error("bar", slog.Any("err", errors.New("error"))) |
| 64 | }, |
| 65 | expected: "[2mlevel=[0m[91mERROR[0m [2mmsg=[0m[97mbar[0m [2merr=[0m[91merror[0m\n", // nolint: staticcheck |
| 66 | }, |
| 67 | { |
| 68 | noColor: true, |
| 69 | run: func(l *slog.Logger) { |
| 70 | l.Error("bar", slog.Any("err", nil)) |
| 71 | }, |
| 72 | expected: "level=ERROR msg=bar err=null\n", |
| 73 | }, |
| 74 | { |
| 75 | noColor: true, |
| 76 | run: func(l *slog.Logger) { |
| 77 | type Foo struct { |
| 78 | N json.Number |
| 79 | } |
| 80 | x := Foo{json.Number(`invalid`)} |
| 81 | l.Error("bar", slog.Any("err", x)) |
| 82 | }, |
| 83 | expected: "level=ERROR msg=bar err={invalid}\n", |
| 84 | }, |
| 85 | { |
| 86 | noColor: true, |
| 87 | run: func(l *slog.Logger) { |
| 88 | l.With(slog.String("with", "true")).Error("bar") |
| 89 | }, |
| 90 | expected: "level=ERROR msg=bar\n", |
| 91 | }, |
| 92 | { |
| 93 | noColor: true, |
| 94 | run: func(l *slog.Logger) { |
| 95 | l.Info("bar", slog.Group("group", slog.String("with", "true"))) |
| 96 | }, |
| 97 | expected: "level=INFO msg=bar group=[{\"Key\":\"with\",\"Value\":{}}]\n", |
| 98 | }, |
| 99 | { |
| 100 | noColor: true, |
| 101 | run: func(l *slog.Logger) { |
| 102 | l.WithGroup("group").Info("bar", slog.String("with", "true")) |
| 103 | }, |
| 104 | expected: "level=INFO msg=bar with=true\n", |
| 105 | }, |
| 106 | { |
| 107 | noColor: true, |
| 108 | run: func(l *slog.Logger) { |
| 109 | h := l.Handler() |
| 110 | r := slog.NewRecord(time.Now(), slog.LevelInfo, "bar", 0) |
| 111 | _ = h.Handle(context.Background(), r) |
| 112 | }, |
| 113 | expected: "level=INFO msg=bar\n", |
| 114 | }, |
| 115 | { |
| 116 | noColor: true, |
| 117 | run: func(l *slog.Logger) { |
| 118 | h := l.Handler() |
| 119 | r := slog.NewRecord(time.Now(), slog.LevelError, "bar", 0) |
| 120 | r.AddAttrs(slog.Any("err", errors.New("error"))) |
| 121 | _ = h.Handle(context.Background(), r) |
| 122 | }, |
| 123 | expected: "level=ERROR msg=bar err=error\n", |
| 124 | }, |
| 125 | } |
| 126 | |
| 127 | for i, tc := range testCases { |
| 128 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 129 | dst := bytes.NewBufferString("") |
| 130 | tc.run(slog.New(newHandler(dst, slog.LevelDebug.Level(), tc.noColor))) |
| 131 | require.Equal(t, tc.expected, dst.String()) |
| 132 | }) |
| 133 | } |
| 134 | } |
| 135 | |