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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package slog
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
"testing"
"time"
"golang.org/x/exp/slog/internal/buffer"
)
func TestJSONHandler(t *testing.T) {
for _, test := range []struct {
name string
opts HandlerOptions
want string
}{
{
"none",
HandlerOptions{},
`{"time":"2000-01-02T03:04:05Z","level":"INFO","msg":"m","a":1,"m":{"b":2}}`,
},
{
"replace",
HandlerOptions{ReplaceAttr: upperCaseKey},
`{"TIME":"2000-01-02T03:04:05Z","LEVEL":"INFO","MSG":"m","A":1,"M":{"b":2}}`,
},
} {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
h := NewJSONHandler(&buf, &test.opts)
r := NewRecord(testTime, LevelInfo, "m", 0)
r.AddAttrs(Int("a", 1), Any("m", map[string]int{"b": 2}))
if err := h.Handle(context.Background(), r); err != nil {
t.Fatal(err)
}
got := strings.TrimSuffix(buf.String(), "\n")
if got != test.want {
t.Errorf("\ngot %s\nwant %s", got, test.want)
}
})
}
}
// for testing json.Marshaler
type jsonMarshaler struct {
s string
}
func (j jsonMarshaler) String() string { return j.s } // should be ignored
func (j jsonMarshaler) MarshalJSON() ([]byte, error) {
if j.s == "" {
return nil, errors.New("json: empty string")
}
return []byte(fmt.Sprintf(`[%q]`, j.s)), nil
}
type jsonMarshalerError struct {
jsonMarshaler
}
func (jsonMarshalerError) Error() string { return "oops" }
func TestAppendJSONValue(t *testing.T) {
// jsonAppendAttrValue should always agree with json.Marshal.
for _, value := range []any{
"hello",
`"[{escape}]"`,
"<escapeHTML&>",
`-123`,
int64(-9_200_123_456_789_123_456),
uint64(9_200_123_456_789_123_456),
-12.75,
1.23e-9,
false,
time.Minute,
testTime,
jsonMarshaler{"xyz"},
jsonMarshalerError{jsonMarshaler{"pqr"}},
LevelWarn,
} {
got := jsonValueString(AnyValue(value))
want, err := marshalJSON(value)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("%v: got %s, want %s", value, got, want)
}
}
}
func marshalJSON(x any) (string, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(x); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}
func TestJSONAppendAttrValueSpecial(t *testing.T) {
// Attr values that render differently from json.Marshal.
for _, test := range []struct {
value any
want string
}{
{math.NaN(), `"!ERROR:json: unsupported value: NaN"`},
{math.Inf(+1), `"!ERROR:json: unsupported value: +Inf"`},
{math.Inf(-1), `"!ERROR:json: unsupported value: -Inf"`},
{io.EOF, `"EOF"`},
} {
got := jsonValueString(AnyValue(test.value))
if got != test.want {
t.Errorf("%v: got %s, want %s", test.value, got, test.want)
}
}
}
func jsonValueString(v Value) string {
var buf []byte
s := &handleState{h: &commonHandler{json: true}, buf: (*buffer.Buffer)(&buf)}
if err := appendJSONValue(s, v); err != nil {
s.appendError(err)
}
return string(buf)
}
func BenchmarkJSONHandler(b *testing.B) {
for _, bench := range []struct {
name string
opts HandlerOptions
}{
{"defaults", HandlerOptions{}},
{"time format", HandlerOptions{
ReplaceAttr: func(_ []string, a Attr) Attr {
v := a.Value
if v.Kind() == KindTime {
return String(a.Key, v.Time().Format(rfc3339Millis))
}
if a.Key == "level" {
return Attr{"severity", a.Value}
}
return a
},
}},
{"time unix", HandlerOptions{
ReplaceAttr: func(_ []string, a Attr) Attr {
v := a.Value
if v.Kind() == KindTime {
return Int64(a.Key, v.Time().UnixNano())
}
if a.Key == "level" {
return Attr{"severity", a.Value}
}
return a
},
}},
} {
b.Run(bench.name, func(b *testing.B) {
l := New(NewJSONHandler(io.Discard, &bench.opts)).With(
String("program", "my-test-program"),
String("package", "log/slog"),
String("traceID", "2039232309232309"),
String("URL", "https://pkg.go.dev/golang.org/x/log/slog"))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(nil, LevelInfo, "this is a typical log message",
String("module", "github.com/google/go-cmp"),
String("version", "v1.23.4"),
Int("count", 23),
Int("number", 123456),
)
}
})
}
}
func BenchmarkPreformatting(b *testing.B) {
type req struct {
Method string
URL string
TraceID string
Addr string
}
structAttrs := []any{
String("program", "my-test-program"),
String("package", "log/slog"),
Any("request", &req{
Method: "GET",
URL: "https://pkg.go.dev/golang.org/x/log/slog",
TraceID: "2039232309232309",
Addr: "127.0.0.1:8080",
}),
}
outFile, err := os.Create(filepath.Join(b.TempDir(), "bench.log"))
if err != nil {
b.Fatal(err)
}
defer func() {
if err := outFile.Close(); err != nil {
b.Fatal(err)
}
}()
for _, bench := range []struct {
name string
wc io.Writer
attrs []any
}{
{"separate", io.Discard, []any{
String("program", "my-test-program"),
String("package", "log/slog"),
String("method", "GET"),
String("URL", "https://pkg.go.dev/golang.org/x/log/slog"),
String("traceID", "2039232309232309"),
String("addr", "127.0.0.1:8080"),
}},
{"struct", io.Discard, structAttrs},
{"struct file", outFile, structAttrs},
} {
b.Run(bench.name, func(b *testing.B) {
l := New(NewJSONHandler(bench.wc, nil)).With(bench.attrs...)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(nil, LevelInfo, "this is a typical log message",
String("module", "github.com/google/go-cmp"),
String("version", "v1.23.4"),
Int("count", 23),
Int("number", 123456),
)
}
})
}
}
func BenchmarkJSONEncoding(b *testing.B) {
value := 3.14
buf := buffer.New()
defer buf.Free()
b.Run("json.Marshal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
by, err := json.Marshal(value)
if err != nil {
b.Fatal(err)
}
buf.Write(by)
*buf = (*buf)[:0]
}
})
b.Run("Encoder.Encode", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := json.NewEncoder(buf).Encode(value); err != nil {
b.Fatal(err)
}
*buf = (*buf)[:0]
}
})
_ = buf
}
|