Skip to content

Commit 80dc3cf

Browse files
committed
feat: rename logrlint to loggercheck
Signed-off-by: Timon Wong <timon86.wang@gmail.com>
1 parent bddc63a commit 80dc3cf

16 files changed

+215
-6
lines changed

.golangci.reference.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,11 @@ linters-settings:
11111111
# Default: 1
11121112
tab-width: 1
11131113

1114+
loggercheck:
1115+
logr: true
1116+
klog: true
1117+
zap: true
1118+
11141119
maintidx:
11151120
# Show functions with maintainability index lower than N.
11161121
# A high index indicates better maintainability (it's kind of the opposite of complexity).
@@ -1939,7 +1944,7 @@ linters:
19391944
- interfacer
19401945
- ireturn
19411946
- lll
1942-
- logrlint
1947+
- loggercheck
19431948
- maintidx
19441949
- makezero
19451950
- maligned
@@ -2044,7 +2049,7 @@ linters:
20442049
- interfacer
20452050
- ireturn
20462051
- lll
2047-
- logrlint
2052+
- loggercheck
20482053
- maintidx
20492054
- makezero
20502055
- maligned

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ require (
9595
github.com/tdakkota/asciicheck v0.1.1
9696
github.com/tetafro/godot v1.4.11
9797
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
98+
github.com/timonwong/loggercheck v0.5.0
9899
github.com/timonwong/logrlint v0.1.0
99100
github.com/tomarrell/wrapcheck/v2 v2.6.2
100101
github.com/tommy-muehle/go-mnd/v2 v2.5.0

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ var defaultLintersSettings = LintersSettings{
6666
LineLength: 120,
6767
TabWidth: 1,
6868
},
69+
LoggerCheck: LoggerCheckSettings{
70+
Logr: true,
71+
Klog: true,
72+
Zap: true,
73+
},
6974
MaintIdx: MaintIdxSettings{
7075
Under: 20,
7176
},
@@ -154,6 +159,7 @@ type LintersSettings struct {
154159
InterfaceBloat InterfaceBloatSettings
155160
Ireturn IreturnSettings
156161
Lll LllSettings
162+
LoggerCheck LoggerCheckSettings
157163
MaintIdx MaintIdxSettings
158164
Makezero MakezeroSettings
159165
Maligned MalignedSettings
@@ -470,6 +476,12 @@ type LllSettings struct {
470476
TabWidth int `mapstructure:"tab-width"`
471477
}
472478

479+
type LoggerCheckSettings struct {
480+
Logr bool `mapstructure:"logr"`
481+
Klog bool `mapstructure:"klog"`
482+
Zap bool `mapstructure:"zap"`
483+
}
484+
473485
type MaintIdxSettings struct {
474486
Under int `mapstructure:"under"`
475487
}

pkg/golinters/loggercheck.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package golinters
2+
3+
import (
4+
"strings"
5+
6+
"github.com/timonwong/loggercheck"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
)
12+
13+
func NewLoggerCheck(settings *config.LoggerCheckSettings) *goanalysis.Linter {
14+
analyzer := loggercheck.NewAnalyzer()
15+
cfg := map[string]map[string]interface{}{}
16+
if settings != nil {
17+
var disabled []string
18+
if !settings.Logr {
19+
disabled = append(disabled, "logr")
20+
}
21+
if !settings.Klog {
22+
disabled = append(disabled, "klog")
23+
}
24+
if !settings.Logr {
25+
disabled = append(disabled, "zap")
26+
}
27+
linterCfg := map[string]interface{}{
28+
"disable": strings.Join(disabled, ","),
29+
}
30+
cfg[analyzer.Name] = linterCfg
31+
}
32+
33+
return goanalysis.NewLinter(
34+
analyzer.Name,
35+
analyzer.Doc,
36+
[]*analysis.Analyzer{analyzer},
37+
cfg,
38+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
39+
}

pkg/lint/lintersdb/manager.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
140140
interfaceBloatCfg *config.InterfaceBloatSettings
141141
ireturnCfg *config.IreturnSettings
142142
lllCfg *config.LllSettings
143+
loggerCheckCfg *config.LoggerCheckSettings
143144
maintIdxCfg *config.MaintIdxSettings
144145
makezeroCfg *config.MakezeroSettings
145146
malignedCfg *config.MalignedSettings
@@ -214,6 +215,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
214215
interfaceBloatCfg = &m.cfg.LintersSettings.InterfaceBloat
215216
ireturnCfg = &m.cfg.LintersSettings.Ireturn
216217
lllCfg = &m.cfg.LintersSettings.Lll
218+
loggerCheckCfg = &m.cfg.LintersSettings.LoggerCheck
217219
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
218220
makezeroCfg = &m.cfg.LintersSettings.Makezero
219221
malignedCfg = &m.cfg.LintersSettings.Maligned
@@ -583,11 +585,19 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
583585
WithSince("v1.8.0").
584586
WithPresets(linter.PresetStyle),
585587

588+
linter.NewConfig(golinters.NewLoggerCheck(loggerCheckCfg)).
589+
WithSince("v1.50.0").
590+
WithLoadForGoAnalysis().
591+
WithPresets(linter.PresetBugs).
592+
WithAlternativeNames("logrlint").
593+
WithURL("https://github.com/timonwong/loggercheck"),
594+
586595
linter.NewConfig(golinters.NewLogrLint()).
587596
WithSince("v1.49.0").
588597
WithLoadForGoAnalysis().
589598
WithPresets(linter.PresetBugs).
590-
WithURL("https://github.com/timonwong/logrlint"),
599+
WithURL("https://github.com/timonwong/logrlint").
600+
Deprecated("The repository of the linter has been archived by the owner.", "v1.50.0", "loggercheck"),
591601

592602
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
593603
WithSince("v1.44.0").

test/linters_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func TestTypecheck(t *testing.T) {
2929

3030
func TestSourcesFromTestdataSubDir(t *testing.T) {
3131
subDirs := []string{
32-
"logrlint",
32+
"loggercheck",
33+
"logrlint", // deprecated linter
3334
}
3435

3536
for _, dir := range subDirs {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
loggercheck:
3+
logr: true
4+
klog: false
5+
zap: false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
loggercheck:
3+
logr: false
4+
klog: false
5+
zap: zap

test/testdata/loggercheck/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module loggercheck
2+
3+
go 1.16
4+
5+
require (
6+
github.com/go-logr/logr v1.2.3
7+
go.uber.org/atomic v1.10.0 // indirect
8+
go.uber.org/multierr v1.8.0 // indirect
9+
go.uber.org/zap v1.23.0
10+
k8s.io/klog/v2 v2.70.1
11+
)

test/testdata/loggercheck/go.sum

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//golangcitest:args -Eloggercheck
2+
package loggercheck
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/go-logr/logr"
8+
"go.uber.org/zap"
9+
"k8s.io/klog/v2"
10+
)
11+
12+
func ExampleAll() {
13+
log := logr.Discard()
14+
log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging`
15+
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
16+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
17+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")
18+
19+
klog.InfoS("message", "key1") // want `odd number of arguments passed as key-value pairs for logging`
20+
21+
sugar := zap.NewExample().Sugar()
22+
defer sugar.Sync()
23+
sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Eloggercheck
2+
//golangcitest:config_path configs/loggercheck_logronly.yml
3+
package loggercheck
4+
5+
import (
6+
"github.com/go-logr/logr"
7+
"k8s.io/klog/v2"
8+
)
9+
10+
func ExampleLogrOnly() {
11+
log := logr.Discard()
12+
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
13+
14+
klog.InfoS("message", "key1")
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//golangcitest:args -Eloggercheck
2+
//golangcitest:config_path configs/loggercheck_zaponly.yml
3+
package loggercheck
4+
5+
import "go.uber.org/zap"
6+
7+
func ExampleZapOnly() {
8+
sugar := zap.NewExample().Sugar()
9+
defer sugar.Sync()
10+
11+
sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
12+
sugar.Errorw("error message", "key1") // want `odd number of arguments passed as key-value pairs for logging`
13+
}

test/testdata/logrlint/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module logrlint
22

33
go 1.16
44

5-
require github.com/go-logr/logr v1.2.3
5+
require github.com/go-logr/logr v1.2.3

test/testdata/logrlint/logrlint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//golangcitest:args -Elogrlint
2-
package logrlint
2+
package loggercheck
33

44
import (
55
"fmt"

0 commit comments

Comments
 (0)