-
Notifications
You must be signed in to change notification settings - Fork 0
/
sloggen_test.go
145 lines (124 loc) · 2.93 KB
/
sloggen_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
package main
import (
"bytes"
"context"
"log/slog"
"path/filepath"
"strings"
"testing"
"go-simpler.org/assert"
. "go-simpler.org/assert/EF"
"go-simpler.org/sloggen/example"
)
var cfg = config{
Pkg: "test",
Imports: []string{"fmt", "log/slog", "strings", "time"},
Levels: map[int]string{1: "custom"},
Consts: []string{"foo"},
Attrs: map[string]string{
"bar": "time.Time",
"baz": "time.Duration",
},
}
func Test_readFlags(t *testing.T) {
args := []string{
"-pkg=test",
"-i=time",
"-l=custom:1",
"-c=foo",
"-a=bar:time.Time",
"-a=baz:time.Duration",
}
got, err := readFlags(args)
assert.NoErr[F](t, err)
assert.Equal[E](t, got, &cfg)
}
func Test_readConfig(t *testing.T) {
r := strings.NewReader(`
pkg: test
imports:
- time
levels:
- custom: 1
consts:
- foo
attrs:
- bar: time.Time
- baz: time.Duration
`)
got, err := readConfig(r)
assert.NoErr[F](t, err)
assert.Equal[E](t, got, &cfg)
}
func Test_writeCode(t *testing.T) {
const src = `// Code generated by go-simpler.org/sloggen. DO NOT EDIT.
package test
import (
"fmt"
"log/slog"
"strings"
"time"
)
const LevelCustom = slog.Level(1)
const Foo = "foo"
func Bar(value time.Time) slog.Attr { return slog.Time("bar", value) }
func Baz(value time.Duration) slog.Attr { return slog.Duration("baz", value) }
func ParseLevel(s string) (slog.Level, error) {
switch strings.ToUpper(s) {
case "CUSTOM":
return LevelCustom, nil
default:
return 0, fmt.Errorf("slog: level string %q: unknown name", s)
}
}
func RenameLevels(_ []string, attr slog.Attr) slog.Attr {
if attr.Key != slog.LevelKey {
return attr
}
switch attr.Value.Any().(slog.Level) {
case LevelCustom:
attr.Value = slog.StringValue("CUSTOM")
}
return attr
}
`
var buf bytes.Buffer
err := writeCode(&buf, &cfg)
assert.NoErr[F](t, err)
assert.Equal[E](t, buf.String(), src)
}
func TestExample(t *testing.T) {
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.TimeKey {
return slog.Attr{}
}
if attr.Key == slog.SourceKey {
src := attr.Value.Any().(*slog.Source)
src.File = filepath.Base(src.File)
}
return example.RenameLevels(groups, attr)
}
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
AddSource: true,
Level: example.LevelInfo,
ReplaceAttr: replaceAttr,
})
logger := example.New(handler).
WithGroup("group").
With(slog.String("key", "value"))
level, err := example.ParseLevel("ALERT")
assert.NoErr[F](t, err)
assert.Equal[E](t, level, example.LevelAlert)
ctx := context.Background()
enabled := logger.Enabled(ctx, level)
assert.Equal[E](t, enabled, true)
logger.Info(ctx, "foo")
logger.Alert(ctx, "bar")
logger.Log(ctx, level, "baz")
assert.Equal[E](t, "\n"+buf.String(), `
level=INFO source=sloggen_test.go:137 msg=foo group.key=value
level=ALERT source=sloggen_test.go:138 msg=bar group.key=value
level=ALERT source=sloggen_test.go:139 msg=baz group.key=value
`)
}