This repository was archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_test.go
159 lines (142 loc) Β· 3.31 KB
/
log_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
// go test -bench=. -benchmem
package slog
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"strings"
"testing"
"time"
)
type logRecord struct {
Level string `json:"level"`
Message string `json:"msg"`
Time time.Time `json:"ts"`
Fields map[string]interface{} `json:"fields"`
}
func BenchmarkSlog(b *testing.B) {
Writer = DiscardWrapper
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Warning(
"fake",
String("str", "foo"),
Int("int", 1),
Int64("int64", 1),
Float64("float64", 11723445172634.12837),
String("string1", "\n"),
String("string2", "π©"),
String("string3", "π€"),
String("string4", "π"),
Bool("bool", true),
)
}
})
}
func BenchmarkStandardJSON(b *testing.B) {
record := logRecord{
Level: "debug",
Message: "fake",
Time: time.Unix(0, 0),
Fields: map[string]interface{}{
"str": "foo",
"int": int(1),
"int64": int64(1),
"float64": float64(11723445172634.12837),
"string1": "\n",
"string2": "π©",
"string3": "π€",
"string4": "π",
"bool": true,
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = json.Marshal(record)
}
})
}
func BenchmarkStandardLog(b *testing.B) {
stdLog := log.New(ioutil.Discard, "", 0)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
stdLog.Printf("level:debug msg:fake time:%s str:%s int:%d int64:%d float64:%f string1:%s string2:%s string3:%s string4:%s bool:%v",
time.Now(), "foo", 1, 1, 1.0, "\n", "π©", "π€", "π", true)
}
})
}
func BenchmarkStandardCombo(b *testing.B) {
stdLog := log.New(ioutil.Discard, "", 0)
record := logRecord{
Level: "debug",
Message: "fake",
Time: time.Unix(0, 0),
Fields: map[string]interface{}{
"str": "foo",
"int": int(1),
"int64": int64(1),
"float64": float64(11723445172634.12837),
"string1": "\n",
"string2": "π©",
"string3": "π€",
"string4": "π",
"bool": true,
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
out, _ := json.Marshal(record)
stdLog.Println(string(out))
}
})
}
func traceErrCaller(msg, msg2 string) {
err := errors.New(msg)
_ = TraceErr(err, String("msg2", msg2))
}
func TestTraceInfo(t *testing.T) {
ogWriter := Writer
var b bytes.Buffer
Writer = traceSyncWrapper{&b}
msg := "foobar"
traceErrCaller(msg, "helloworld")
Writer = ogWriter
if !strings.Contains(b.String(), "traceErrCaller") {
t.Fatal()
}
if !strings.Contains(b.String(), "foobar") {
t.Fatal()
}
if !strings.Contains(b.String(), "helloworld") {
t.Fatal()
}
}
type traceSyncWrapper struct {
io.Writer
}
func (t traceSyncWrapper) Sync() error {
return nil
}
func TestRawJSON(t *testing.T) {
ogWriter := Writer
var b bytes.Buffer
Writer = traceSyncWrapper{&b}
Info("testing raw json", RawJSON("raw", []byte(`{"foo":"bar","context":{"one":1,"two":2}}`)))
Writer = ogWriter
// We expect the output, but trim the time field off.
expected := `{"level":"info", "msg":"testing raw json", "raw":{"foo":"bar","context":{"one":1,"two":2}},`
if !strings.Contains(b.String(), expected) {
t.Fatal()
}
// Ensure it's valid json.
var jData map[string]interface{}
err := json.Unmarshal(b.Bytes(), &jData)
if err != nil {
t.Fatal()
}
}