File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change 5
5
[ ![ goreportcard] ( https://goreportcard.com/badge/go-simpler.org/slog-gen )] ( https://goreportcard.com/report/go-simpler.org/slog-gen )
6
6
[ ![ codecov] ( https://codecov.io/gh/go-simpler/slog-gen/branch/main/graph/badge.svg )] ( https://codecov.io/gh/go-simpler/slog-gen )
7
7
8
- Generate domain-specific ` slog.Attr ` constructors.
8
+ ## 📌 About
9
+
10
+ When using ` log/slog ` in a production-grade project, it is useful to write helpers to avoid human error in the keys.
11
+
12
+ ``` go
13
+ slog.Info (" a user has logged in" , " user_id" , 42 )
14
+ slog.Info (" a user has logged out" , " user_ip" , 42 ) // oops :(
15
+ ```
16
+
17
+ Depending on your code style, these can be simple constants (if you prefer key-value arguments)...
18
+
19
+ ``` go
20
+ const UserId = " user_id"
21
+ ```
22
+
23
+ ...or constructors for ` slog.Attr ` (if you're a safety/performance advocate).
24
+
25
+ ``` go
26
+ func UserId (value int ) slog .Attr {
27
+ return slog.Int (" user_id" , value)
28
+ }
29
+ ```
30
+
31
+ ` slog-gen ` generates such code for you based on a simple config (a single source of truth),
32
+ which makes it easy to share domain-specific helpers between related (micro)services.
33
+
34
+ ## 📦 Install
35
+
36
+ Create and fill in the ` .slog.yml ` config based on the example,
37
+ then add the following directive to any ` .go ` file and run ` go generate ./... ` .
38
+
39
+ ``` go
40
+ // go:generate go run go-simpler.org/slog-gen --config=.slog.yml
41
+ ```
42
+
43
+ To get started, see the ` .slog.example.yml ` file and the ` example ` directory.
Original file line number Diff line number Diff line change 9
9
. "go-simpler.org/assert/dotimport"
10
10
)
11
11
12
- //go:generate go run main.go codegen.go -config=.slog.example.yml
12
+ //go:generate go run main.go codegen.go -- config=.slog.example.yml
13
13
14
14
func Test_readConfig (t * testing.T ) {
15
15
r := strings .NewReader (`
You can’t perform that action at this time.
0 commit comments