-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger_test.go
189 lines (163 loc) · 4.02 KB
/
logger_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package clog
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"reflect"
"testing"
)
var (
testopts = &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Ignore time to make testing easier.
if a.Key == "time" {
return slog.Attr{}
}
return a
},
}
)
func TestLogger(t *testing.T) {
ctx := context.Background()
b := new(bytes.Buffer)
base := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
log := NewLogger(base).With("a", "b")
log.InfoContext(ctx, "")
t.Log(b.String())
want := map[string]any{
"level": "INFO",
"msg": "",
"a": "b",
}
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %v, got %v", want, got)
}
}
func TestLoggerNilBase(t *testing.T) {
log := NewLogger(nil)
log.Info("")
}
func TestLoggerFromContext(t *testing.T) {
b := new(bytes.Buffer)
base := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
log := NewLogger(base).With("a", "b")
ctx := WithLogger(context.Background(), log)
FromContext(ctx).Info("")
want := map[string]any{
"level": "INFO",
"msg": "",
"a": "b",
}
t.Run("FromContext.Info", func(t *testing.T) {
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %v, got %v", want, got)
}
})
b.Reset()
t.Run("clog.Info", func(t *testing.T) {
InfoContext(ctx, "")
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %v, got %v", want, got)
}
})
}
func TestLoggerPC(t *testing.T) {
b := new(bytes.Buffer)
log := NewLogger(slog.New(NewHandler(slog.NewJSONHandler(b, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: testopts.ReplaceAttr,
}))))
log.Info("")
t.Log(b.String())
var got struct {
Source struct {
File string `json:"file"`
Function string `json:"function"`
} `json:"source"`
}
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
// Knowing that the PC is from this test is good enough.
want := fmt.Sprintf("github.com/chainguard-dev/clog.%s", t.Name())
if got.Source.Function != want {
t.Errorf("want %v, got %v", want, got)
}
}
func TestWith(t *testing.T) {
ctx := context.WithValue(context.Background(), "test", "test")
log := NewLoggerWithContext(ctx, nil)
withed := log.With("a", "b")
if want := withed.ctx; want != ctx {
t.Errorf("want %v, got %v", want, ctx)
}
withed = log.WithGroup("a")
if want := withed.ctx; want != ctx {
t.Errorf("want %v, got %v", want, ctx)
}
}
func TestDefaultHandler(t *testing.T) {
old := slog.Default()
defer func() {
slog.SetDefault(old)
}()
b := new(bytes.Buffer)
slog.SetDefault(slog.New(slog.NewJSONHandler(b, testopts)))
t.Run("Info", func(t *testing.T) {
FromContext(WithValues(context.Background(), "a", "b")).Info("")
want := map[string]any{
"level": "INFO",
"msg": "",
"a": "b",
}
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %v, got %v", want, got)
}
})
b.Reset()
t.Run("InfoContext", func(t *testing.T) {
// Set logger with original value
ctx := WithValues(context.Background(), "a", "b")
logger := FromContext(ctx)
// Override value in request context - we expect this to overwrite the original value set in the logger
logger.InfoContext(WithValues(ctx, "a", "c"), "")
want := map[string]any{
"level": "INFO",
"msg": "",
"a": "c",
}
var got map[string]any
if err := json.Unmarshal(b.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %v, got %v", want, got)
}
})
b.Reset()
t.Run("Debug - no log", func(t *testing.T) {
logger := FromContext(context.Background())
logger.Debug("asdf")
if b.Len() != 0 {
t.Errorf("want empty, got %q", b.String())
}
})
}