@@ -31,25 +31,36 @@ The linter has several options, so you can adjust it to your own code style.
31
31
32
32
## 📦 Install
33
33
34
- Download a prebuilt binary from the [ Releases ] [ 1 ] page .
34
+ ` sloglint ` is integrated into [ ` golangci-lint ` ] [ 1 ] , and this is the recommended way to use it .
35
35
36
- ## 📋 Usage
36
+ To enable the linter, add the following lines to ` .golangci.yml ` :
37
37
38
- ``` shell
39
- sloglint [flags] ./...
38
+ ``` yaml
39
+ linters :
40
+ enable :
41
+ - sloglint
40
42
` ` `
41
43
44
+ Alternatively, you can download a prebuilt binary from the [Releases][2] page to use ` sloglint` standalone.
45
+
46
+ # # 📋 Usage
47
+
48
+ Run `golangci-lint` with `sloglint` enabled.
49
+ See the list of [available options][3] to configure the linter.
50
+
51
+ When using `sloglint` standalone, pass the options as flags of the same name.
52
+
42
53
# ## Key-value pairs only
43
54
44
- The ` - kv-only` flag causes ` sloglint ` to report any use of attributes:
55
+ The `kv-only` option causes `sloglint` to report any use of attributes :
45
56
46
57
` ` ` go
47
58
slog.Info("a user has logged in", slog.Int("user_id", 42)) // sloglint: attributes should not be used
48
59
` ` `
49
60
50
61
# ## Attributes only
51
62
52
- In contrast, the ` - attr-only` flag causes ` sloglint ` to report any use of key-value pairs:
63
+ In contrast, the `attr-only` option causes `sloglint` to report any use of key-value pairs :
53
64
54
65
` ` ` go
55
66
slog.Info("a user has logged in", "user_id", 42) // sloglint: key-value pairs should not be used
@@ -59,7 +70,7 @@ slog.Info("a user has logged in", "user_id", 42) // sloglint: key-value pairs sh
59
70
60
71
Some `slog.Handler` implementations make use of the given `context.Context` (e.g. to access context values).
61
72
For them to work properly, you need to pass a context to all logger calls.
62
- The ` - context-only` flag causes ` sloglint ` to report the use of methods without a context:
73
+ The `context-only` option causes `sloglint` to report the use of methods without a context :
63
74
64
75
` ` ` go
65
76
slog.Info("a user has logged in") // sloglint: methods without a context should not be used
@@ -74,7 +85,7 @@ slog.InfoContext(ctx, "a user has logged in")
74
85
# ## No raw keys
75
86
76
87
To prevent typos, you may want to forbid the use of raw keys altogether.
77
- The ` - no-raw-keys` flag causes ` sloglint ` to report the use of strings as keys
88
+ The `no-raw-keys` option causes `sloglint` to report the use of strings as keys
78
89
(including `slog.Attr` calls, e.g. `slog.Int("user_id", 42)`) :
79
90
80
91
` ` ` go
@@ -97,21 +108,23 @@ func UserId(value int) slog.Attr { return slog.Int("user_id", value) }
97
108
slog.Info("a user has logged in", UserId(42))
98
109
` ` `
99
110
100
- > 💡 Such helpers can be automatically generated for you by the [ ` sloggen ` ] [ 2 ] tool. Give it a try too!
111
+ > 💡 Such helpers can be automatically generated for you by the [`sloggen`][4 ] tool. Give it a try too!
101
112
102
113
# ## Key naming convention
103
114
104
115
To ensure consistency in logs, you may want to enforce a single key naming convention.
105
- The ` - key-naming-case=<snake|kebab|camel|pascal> ` flag causes ` sloglint ` to report keys written in a case other than the given one:
116
+ The `key-naming-case` option causes `sloglint` to report keys written in a case other than the given one :
106
117
107
118
` ` ` go
108
119
slog.Info("a user has logged in", "user-id", 42) // sloglint: keys should be written in snake_case
109
120
` ` `
110
121
122
+ Possible values are `snake`, `kebab`, `camel`, or `pascal`.
123
+
111
124
# ## Arguments on separate lines
112
125
113
126
To improve code readability, you may want to put arguments on separate lines, especially when using key-value pairs.
114
- The ` - args-on-sep-lines` flag causes ` sloglint ` to report 2+ arguments on the same line:
127
+ The `args-on-sep-lines` option causes `sloglint` to report 2+ arguments on the same line :
115
128
116
129
` ` ` go
117
130
slog.Info("a user has logged in", "user_id", 42, "ip_address", "192.0.2.0") // sloglint: arguments should be put on separate lines
@@ -126,5 +139,7 @@ slog.Info("a user has logged in",
126
139
)
127
140
` ` `
128
141
129
- [ 1 ] : https://github.com/go-simpler/sloglint/releases
130
- [ 2 ] : https://github.com/go-simpler/sloggen
142
+ [1] : https://golangci-lint.run
143
+ [2] : https://github.com/go-simpler/sloglint/releases
144
+ [3] : https://golangci-lint.run/usage/linters/#sloglint
145
+ [4] : https://github.com/go-simpler/sloggen
0 commit comments