forked from charmbracelet/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_121_test.go
151 lines (142 loc) · 2.82 KB
/
logger_121_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//go:build go1.21
// +build go1.21
package log
import (
"bytes"
"testing"
"log/slog"
"github.com/stretchr/testify/assert"
)
func TestSlogSimple(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
h.SetLevel(DebugLevel)
l := slog.New(h)
cases := []struct {
name string
expected string
msg string
attrs []any
print func(string, ...any)
}{
{
name: "slog debug",
expected: "DEBU slog debug\n",
msg: "slog debug",
print: l.Debug,
attrs: nil,
},
{
name: "slog info",
expected: "INFO slog info\n",
msg: "slog info",
print: l.Info,
attrs: nil,
},
{
name: "slog warn",
expected: "WARN slog warn\n",
msg: "slog warn",
print: l.Warn,
attrs: nil,
},
{
name: "slog error",
expected: "ERRO slog error\n",
msg: "slog error",
print: l.Error,
attrs: nil,
},
{
name: "slog error attrs",
expected: "ERRO slog error foo=bar\n",
msg: "slog error",
print: l.Error,
attrs: []any{"foo", "bar"},
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.print(c.msg, c.attrs...)
assert.Equal(t, c.expected, buf.String())
})
}
}
func TestSlogWith(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
h.SetLevel(DebugLevel)
l := slog.New(h).With("a", "b")
cases := []struct {
name string
expected string
msg string
attrs []any
print func(string, ...any)
}{
{
name: "slog debug",
expected: "DEBU slog debug a=b foo=bar\n",
msg: "slog debug",
print: l.Debug,
attrs: []any{"foo", "bar"},
},
{
name: "slog info",
expected: "INFO slog info a=b foo=bar\n",
msg: "slog info",
print: l.Info,
attrs: []any{"foo", "bar"},
},
{
name: "slog warn",
expected: "WARN slog warn a=b foo=bar\n",
msg: "slog warn",
print: l.Warn,
attrs: []any{"foo", "bar"},
},
{
name: "slog error",
expected: "ERRO slog error a=b foo=bar\n",
msg: "slog error",
print: l.Error,
attrs: []any{"foo", "bar"},
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.print(c.msg, c.attrs...)
assert.Equal(t, c.expected, buf.String())
})
}
}
func TestSlogWithGroup(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
l := slog.New(h).WithGroup("charm").WithGroup("bracelet")
cases := []struct {
name string
expected string
msg string
}{
{
name: "simple",
msg: "message",
expected: "INFO charm.bracelet: message\n",
},
{
name: "empty",
msg: "",
expected: "INFO charm.bracelet:\n",
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
l.Info(c.msg)
assert.Equal(t, c.expected, buf.String())
})
}
}