Skip to content

Commit

Permalink
docs: add project description
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Sep 10, 2023
1 parent e629585 commit 8095757
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,39 @@
[![goreportcard](https://goreportcard.com/badge/go-simpler.org/slog-gen)](https://goreportcard.com/report/go-simpler.org/slog-gen)
[![codecov](https://codecov.io/gh/go-simpler/slog-gen/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/slog-gen)

Generate domain-specific `slog.Attr` constructors.
## 📌 About

When using `log/slog` in a production-grade project, it is useful to write helpers to avoid human error in the keys.

```go
slog.Info("a user has logged in", "user_id", 42)
slog.Info("a user has logged out", "user_ip", 42) // oops :(
```

Depending on your code style, these can be simple constants (if you prefer key-value arguments)...

```go
const UserId = "user_id"
```

...or constructors for `slog.Attr` (if you're a safety/performance advocate).

```go
func UserId(value int) slog.Attr {
return slog.Int("user_id", value)
}
```

`slog-gen` generates such code for you based on a simple config (a single source of truth),
which makes it easy to share domain-specific helpers between related (micro)services.

## 📦 Install

Create and fill in the `.slog.yml` config based on the example,
then add the following directive to any `.go` file and run `go generate ./...`.

```go
//go:generate go run go-simpler.org/slog-gen --config=.slog.yml
```

To get started, see the `.slog.example.yml` file and the `example` directory.
2 changes: 1 addition & 1 deletion codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
. "go-simpler.org/assert/dotimport"
)

//go:generate go run main.go codegen.go -config=.slog.example.yml
//go:generate go run main.go codegen.go --config=.slog.example.yml

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

0 comments on commit 8095757

Please sign in to comment.