Skip to content

Commit 2693fb5

Browse files
authored
docs: update README
Signed-off-by: Tom <[email protected]>
1 parent 5a7f257 commit 2693fb5

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,96 @@
77

88
A Go linter that ensures consistent code style when using `log/slog`.
99

10+
## 📌 About
11+
12+
The `log/slog` API allows two different types of arguments: key-value pairs and attributes.
13+
People may have different opinions about which one is better,
14+
but nobody probably wants to mix them up because it makes the code harder to read.
15+
16+
```go
17+
slog.Info("a user has logged in", "user_id", 42, slog.String("ip_address", "192.0.2.0")) // ugh
18+
```
19+
20+
`sloglint` finds such function calls and checks that all the arguments are either key-value pairs or attributes.
21+
The linter has several options, so you can adjust it to your own code style.
22+
1023
## 🚀 Features
1124

12-
* Forbid mixing key-value pairs and attributes in a single function call (default)
25+
* Forbid mixing key-value pairs and attributes within a single function call (default)
1326
* Enforce using either key-value pairs or attributes for the entire project (optional)
1427
* Enforce using constants instead of raw keys (optional)
1528
* Enforce putting arguments on separate lines (optional)
29+
30+
## 📦 Install
31+
32+
Download a prebuilt binary from the [Releases][1] page.
33+
34+
## 📋 Usage
35+
36+
```shell
37+
sloglint [flags] ./...
38+
```
39+
40+
### Key-value pairs only
41+
42+
The `-kv-only` flag causes `sloglint` to report any use of attributes.
43+
44+
```go
45+
slog.Info("a user has logged in", slog.Int("user_id", 42)) // sloglint: attributes should not be used
46+
```
47+
48+
### Attributes only
49+
50+
In contrast, the `-attr-only` flag causes `sloglint` to report any use of key-value pairs.
51+
52+
```go
53+
slog.Info("a user has logged in", "user_id", 42) // sloglint: key-value pairs should not be used
54+
```
55+
56+
### No raw keys
57+
58+
To prevent typos, you may want to forbid the use of raw keys altogether.
59+
The `-no-raw-keys` flag causes `sloglint` to report the use of strings as keys (including `slog.Attr` calls, e.g. `slog.Int("user_id", 42)`).
60+
61+
```go
62+
slog.Info("a user has logged in", "user_id", 42) // sloglint: raw keys should not be used
63+
```
64+
65+
This report can be fixed by using either constants...
66+
67+
```go
68+
const UserId = "user_id"
69+
70+
slog.Info("a user has logged in", UserId, 42)
71+
```
72+
73+
...or custom `slog.Attr` constructors.
74+
75+
```go
76+
func UserId(value int) slog.Attr { return slog.Int("user_id", value) }
77+
78+
slog.Info("a user has logged in", UserId(42))
79+
```
80+
81+
> 💡 Such helpers can be automatically generated for you by the [`sloggen`][2] tool. Give it a try too!
82+
83+
### Arguments on separate lines
84+
85+
To improve code readability, you may want to put arguments on separate lines, especially when using key-value pairs.
86+
The `-args-on-sep-lines` flag causes `sloglint` to report 2+ arguments on the same line.
87+
88+
```go
89+
slog.Info("a user has logged in", "user_id", 42, "ip_address", "192.0.2.0") // sloglint: arguments should be put on separate lines
90+
```
91+
92+
This report can be fixed by reformatting the code.
93+
94+
```go
95+
slog.Info("a user has logged in",
96+
"user_id", 42,
97+
"ip_address", "192.0.2.0",
98+
)
99+
```
100+
101+
[1]: https://github.com/go-simpler/sloglint/releases
102+
[2]: https://github.com/go-simpler/sloggen

0 commit comments

Comments
 (0)