Serge Bazanski | 6c8ee0b | 2023-04-05 12:29:57 +0200 | [diff] [blame^] | 1 | package logtree |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "go.uber.org/zap" |
| 7 | ) |
| 8 | |
| 9 | func TestZapify(t *testing.T) { |
| 10 | lt := New() |
| 11 | |
| 12 | z := Zapify(lt.MustLeveledFor("zap"), zap.InfoLevel) |
| 13 | z.Info("foo", zap.String("strp", "strv"), zap.Int("intp", 42)) |
| 14 | z.Warn("foo!", zap.String("strp", "strv"), zap.Int("intp", 1337)) |
| 15 | z.Error("foo!!") |
| 16 | |
| 17 | res, err := lt.Read("zap", WithBacklog(BacklogAllAvailable)) |
| 18 | if err != nil { |
| 19 | t.Fatalf("Read: %v", err) |
| 20 | } |
| 21 | defer res.Close() |
| 22 | |
| 23 | if want, got := 3, len(res.Backlog); want != got { |
| 24 | t.Errorf("Wanted %d entries, got %d", want, got) |
| 25 | } else { |
| 26 | for i, te := range []struct { |
| 27 | msg string |
| 28 | sev Severity |
| 29 | }{ |
| 30 | {`foo {"intp":42,"strp":"strv"}`, INFO}, |
| 31 | {`foo! {"intp":1337,"strp":"strv"}`, WARNING}, |
| 32 | {`foo!!`, ERROR}, |
| 33 | } { |
| 34 | if want, got := te.msg, res.Backlog[i].Leveled.messages[0]; want != got { |
| 35 | t.Errorf("Line %d: wanted message %q, got %q", i, want, got) |
| 36 | } |
| 37 | if want, got := te.sev, res.Backlog[i].Leveled.severity; want != got { |
| 38 | t.Errorf("Line %d: wanted level %s, got %s", i, want, got) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |