Skip to content

Commit 2410c01

Browse files
authored
docs: add project description (#5)
1 parent e629585 commit 2410c01

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,39 @@
55
[![goreportcard](https://goreportcard.com/badge/go-simpler.org/slog-gen)](https://goreportcard.com/report/go-simpler.org/slog-gen)
66
[![codecov](https://codecov.io/gh/go-simpler/slog-gen/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/slog-gen)
77

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.

codegen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
. "go-simpler.org/assert/dotimport"
1010
)
1111

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
1313

1414
func Test_readConfig(t *testing.T) {
1515
r := strings.NewReader(`

0 commit comments

Comments
 (0)