blob: b03b8cf41d82abcf30426d03e4e1d5d2cae95917 [file] [log] [blame]
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +02001package logtree
2
3import (
4 "testing"
5
6 "go.uber.org/zap"
Serge Bazanski3c5d0632024-09-12 10:49:12 +00007
8 "source.monogon.dev/go/logging"
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +02009)
10
11func TestZapify(t *testing.T) {
12 lt := New()
13
14 z := Zapify(lt.MustLeveledFor("zap"), zap.InfoLevel)
15 z.Info("foo", zap.String("strp", "strv"), zap.Int("intp", 42))
16 z.Warn("foo!", zap.String("strp", "strv"), zap.Int("intp", 1337))
17 z.Error("foo!!")
18
19 res, err := lt.Read("zap", WithBacklog(BacklogAllAvailable))
20 if err != nil {
21 t.Fatalf("Read: %v", err)
22 }
23 defer res.Close()
24
25 if want, got := 3, len(res.Backlog); want != got {
26 t.Errorf("Wanted %d entries, got %d", want, got)
27 } else {
28 for i, te := range []struct {
29 msg string
Serge Bazanski3c5d0632024-09-12 10:49:12 +000030 sev logging.Severity
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +020031 }{
Serge Bazanski3c5d0632024-09-12 10:49:12 +000032 {`foo {"intp":42,"strp":"strv"}`, logging.INFO},
33 {`foo! {"intp":1337,"strp":"strv"}`, logging.WARNING},
34 {`foo!!`, logging.ERROR},
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +020035 } {
36 if want, got := te.msg, res.Backlog[i].Leveled.messages[0]; want != got {
37 t.Errorf("Line %d: wanted message %q, got %q", i, want, got)
38 }
39 if want, got := te.sev, res.Backlog[i].Leveled.severity; want != got {
40 t.Errorf("Line %d: wanted level %s, got %s", i, want, got)
41 }
42 }
43 }
44}