Skip to content

Commit 4359606

Browse files
authored
feat: update to go 1.21.0, add log/slog support (#72)
* feat: update to go 1.21.0, add log/slog support * fix: update ci go version to 1.21 * ci: make golangci-lint happy * test: add slog test example * fix: support slog group Attr * refactor: make linter happy * test: slog add other Attr example
1 parent d6826ff commit 4359606

File tree

13 files changed

+129
-50
lines changed

13 files changed

+129
-50
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
go: ['1.18', '1.19']
14+
go: ['1.21']
1515
steps:
1616
- uses: actions/checkout@v3
1717
- name: Set up Go

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ jobs:
1515
- name: Set up Go
1616
uses: actions/setup-go@v4
1717
with:
18-
go-version: '1.19'
18+
go-version: '1.21'
1919

2020
- name: Run golangci-lint
2121
uses: golangci/[email protected]
2222
with:
23-
version: v1.49.0
23+
version: v1.54.0
2424
# skip cache becausez of flaky behaviors
2525
skip-build-cache: true
2626
skip-pkg-cache: true

.github/workflows/tag.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Install Go
1313
uses: actions/setup-go@v4
1414
with:
15-
go-version: 1.19
15+
go-version: 1.21
1616
- name: Unshallow
1717
run: git fetch --prune --unshallow
1818
- name: Create release

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module github.com/timonwong/loggercheck
22

3-
go 1.19
3+
go 1.21
44

55
require (
6-
github.com/stretchr/testify v1.8.2
7-
golang.org/x/tools v0.11.0
6+
github.com/stretchr/testify v1.8.4
7+
golang.org/x/tools v0.12.0
88
)
99

1010
require (
1111
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
1313
golang.org/x/mod v0.12.0 // indirect
14-
golang.org/x/sys v0.10.0 // indirect
14+
golang.org/x/sys v0.11.0 // indirect
1515
gopkg.in/yaml.v3 v3.0.1 // indirect
1616
)

go.sum

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
54
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
137
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
148
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
159
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
16-
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
17-
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18-
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
19-
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
10+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
11+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
12+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
14+
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
2015
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2116
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
22-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2317
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2418
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/checkers/filter.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package checkers
2+
3+
import (
4+
"go/ast"
5+
"go/types"
6+
7+
"golang.org/x/tools/go/analysis"
8+
)
9+
10+
func filterKeyAndValues(pass *analysis.Pass, keyAndValues []ast.Expr, objName string) []ast.Expr {
11+
// Check the argument count
12+
filtered := make([]ast.Expr, 0, len(keyAndValues))
13+
for _, arg := range keyAndValues {
14+
// Skip any object type field we found
15+
switch arg := arg.(type) {
16+
case *ast.CallExpr, *ast.Ident:
17+
typ := pass.TypesInfo.TypeOf(arg)
18+
switch typ := typ.(type) {
19+
case *types.Named:
20+
obj := typ.Obj()
21+
if obj != nil && obj.Name() == objName {
22+
continue
23+
}
24+
default:
25+
// pass
26+
}
27+
}
28+
29+
filtered = append(filtered, arg)
30+
}
31+
32+
return filtered
33+
}

internal/checkers/slog.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package checkers
2+
3+
import (
4+
"go/ast"
5+
6+
"golang.org/x/tools/go/analysis"
7+
)
8+
9+
type Slog struct {
10+
General
11+
}
12+
13+
func (z Slog) FilterKeyAndValues(pass *analysis.Pass, keyAndValues []ast.Expr) []ast.Expr {
14+
// check slog.Group() constructed group slog.Attr
15+
// since we also check `slog.Group` so it is OK skip here
16+
return filterKeyAndValues(pass, keyAndValues, "Attr")
17+
}
18+
19+
var _ Checker = (*Slog)(nil)

internal/checkers/zap.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package checkers
22

33
import (
44
"go/ast"
5-
"go/types"
65

76
"golang.org/x/tools/go/analysis"
87
)
@@ -12,31 +11,11 @@ type Zap struct {
1211
}
1312

1413
func (z Zap) FilterKeyAndValues(pass *analysis.Pass, keyAndValues []ast.Expr) []ast.Expr {
15-
// Check the argument count
16-
filtered := make([]ast.Expr, 0, len(keyAndValues))
17-
for _, arg := range keyAndValues {
18-
// Skip any zapcore.Field we found
19-
switch arg := arg.(type) {
20-
case *ast.CallExpr, *ast.Ident:
21-
typ := pass.TypesInfo.TypeOf(arg)
22-
switch typ := typ.(type) {
23-
case *types.Named:
24-
obj := typ.Obj()
25-
// This is a strongly-typed field. Consume it and move on.
26-
// Actually it's go.uber.org/zap/zapcore.Field, however for simplicity
27-
// we don't check the import path
28-
if obj != nil && obj.Name() == "Field" {
29-
continue
30-
}
31-
default:
32-
// pass
33-
}
34-
}
35-
36-
filtered = append(filtered, arg)
37-
}
38-
39-
return filtered
14+
// Skip any zapcore.Field we found
15+
// This is a strongly-typed field. Consume it and move on.
16+
// Actually it's go.uber.org/zap/zapcore.Field, however for simplicity
17+
// we don't check the import path
18+
return filterKeyAndValues(pass, keyAndValues, "Field")
4019
}
4120

4221
var _ Checker = (*Zap)(nil)

loggercheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func newLoggerCheck(opts ...Option) *loggercheck {
5454
}
5555

5656
fs.StringVar(&l.ruleFile, "rulefile", "", "path to a file contains a list of rules")
57-
fs.Var(&l.disable, "disable", "comma-separated list of disabled logger checker (kitlog,klog,logr,zap)")
57+
fs.Var(&l.disable, "disable", "comma-separated list of disabled logger checker (kitlog,klog,logr,zap,slog)")
5858
fs.BoolVar(&l.requireStringKey, "requirestringkey", false, "require all logging keys to be inlined constant strings")
5959
fs.BoolVar(&l.noPrintfLike, "noprintflike", false, "require printf-like format specifier not present in args")
6060

loggercheck_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type dummyTestingErrorf struct {
1515
*testing.T
1616
}
1717

18-
func (t dummyTestingErrorf) Errorf(format string, args ...interface{}) {}
18+
func (t dummyTestingErrorf) Errorf(_ string, _ ...interface{}) {}
1919

2020
func TestLinter(t *testing.T) {
2121
testdata := analysistest.TestData()

staticrules.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,38 @@ var (
3939
"github.com/go-kit/log.WithSuffix",
4040
"(github.com/go-kit/log.Logger).Log",
4141
}),
42+
mustNewStaticRuleSet("slog", []string{
43+
"log/slog.Group",
44+
45+
"log/slog.With",
46+
47+
"log/slog.Debug",
48+
"log/slog.Info",
49+
"log/slog.Warn",
50+
"log/slog.Error",
51+
52+
"log/slog.DebugContext",
53+
"log/slog.InfoContext",
54+
"log/slog.WarnContext",
55+
"log/slog.ErrorContext",
56+
57+
"(*log/slog.Logger).With",
58+
59+
"(*log/slog.Logger).Debug",
60+
"(*log/slog.Logger).Info",
61+
"(*log/slog.Logger).Warn",
62+
"(*log/slog.Logger).Error",
63+
64+
"(*log/slog.Logger).DebugContext",
65+
"(*log/slog.Logger).InfoContext",
66+
"(*log/slog.Logger).WarnContext",
67+
"(*log/slog.Logger).ErrorContext",
68+
}),
4269
}
4370
checkerByRulesetName = map[string]checkers.Checker{
4471
// by default, checkers.General will be used.
45-
"zap": checkers.Zap{},
72+
"zap": checkers.Zap{},
73+
"slog": checkers.Slog{},
4674
}
4775
)
4876

testdata/src/a/all/example.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log/slog"
8+
"time"
79

810
kitlog "github.com/go-kit/log"
911
"github.com/go-logr/logr"
@@ -111,3 +113,22 @@ func ExampleGokitLog() {
111113
kitlog.With(logger, "key1", "value1").Log("msg", "message")
112114
kitlog.With(logger, "key1").Log("msg", "message") // want `odd number of arguments passed as key-value pairs for logging`
113115
}
116+
117+
func ExampleSlog() {
118+
logger := slog.With("key1", "value1")
119+
120+
slog.Info("msg", "key1", "value1")
121+
logger.Info("msg", "key1", "value1")
122+
logger.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
123+
slog.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
124+
logger.InfoContext(context.TODO(), "msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
125+
126+
slog.With("key1", "value1").Info("msg")
127+
slog.With("key1", "value1").Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
128+
129+
slog.Info("with group ok", "key1", "value1", slog.Group("group_val_key", "gkey1", "gvalue1", "gkey2", "gvalue2"))
130+
slog.Info("with group missing val", "key1", "value1", slog.Group("group_val_key", "gkey1", "gvalue1", "gkey2")) // want `odd number of arguments passed as key-value pairs for logging`
131+
132+
slog.Info("with attributes", slog.Time("time", time.Now()), slog.String("method", "POST"), slog.Int("status", 301))
133+
slog.Info("with attributes missing val", slog.Time("time", time.Now()), slog.String("method", "POST"), slog.Int("status", 301), "key_only") // want `odd number of arguments passed as key-value pairs for logging`
134+
}

testdata/src/a/go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
2+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
23
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
45
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -10,18 +11,22 @@ github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
1011
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
1112
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1213
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
14+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1315
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1416
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1517
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1618
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1719
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
20+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1821
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
1922
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
2023
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
24+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
2125
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
2226
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
2327
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
2428
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
2529
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
30+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2631
k8s.io/klog/v2 v2.70.1 h1:7aaoSdahviPmR+XkS7FyxlkkXs6tHISSG03RxleQAVQ=
2732
k8s.io/klog/v2 v2.70.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=

0 commit comments

Comments
 (0)