Skip to content

Commit 3a2ad90

Browse files
authored
feat: rename logrlint to loggercheck (#3144)
1 parent 19a3387 commit 3a2ad90

26 files changed

+454
-37
lines changed

.golangci.reference.yml

+28-2
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,32 @@ linters-settings:
11191119
# Default: 1
11201120
tab-width: 1
11211121

1122+
loggercheck:
1123+
# Allow check for the github.com/go-kit/log library.
1124+
# Default: true
1125+
kitlog: false
1126+
# Allow check for the k8s.io/klog/v2 library.
1127+
# Default: true
1128+
klog: false
1129+
# Allow check for the github.com/go-logr/logr library.
1130+
# Default: true
1131+
logr: false
1132+
# Allow check for the "sugar logger" from go.uber.org/zap library.
1133+
# Default: true
1134+
zap: false
1135+
# Require all logging keys to be inlined constant strings.
1136+
# Default: false
1137+
require-string-key: true
1138+
# Require printf-like format specifier (%s, %d for example) not present.
1139+
# Default: false
1140+
no-printf-like: true
1141+
# List of custom rules to check against, where each rule is a single logger pattern, useful for wrapped loggers.
1142+
# For example: https://github.com/timonwong/loggercheck/blob/7395ab86595781e33f7afba27ad7b55e6956ebcd/testdata/custom-rules.txt
1143+
# Default: empty
1144+
rules:
1145+
- k8s.io/klog/v2.InfoS # package level exported functions
1146+
- (github.com/go-logr/logr.Logger).Error # "Methods"
1147+
- (*go.uber.org/zap.SugaredLogger).With # Also "Methods", but with a pointer receiver
11221148
maintidx:
11231149
# Show functions with maintainability index lower than N.
11241150
# A high index indicates better maintainability (it's kind of the opposite of complexity).
@@ -1956,7 +1982,7 @@ linters:
19561982
- interfacer
19571983
- ireturn
19581984
- lll
1959-
- logrlint
1985+
- loggercheck
19601986
- maintidx
19611987
- makezero
19621988
- maligned
@@ -2061,7 +2087,7 @@ linters:
20612087
- interfacer
20622088
- ireturn
20632089
- lll
2064-
- logrlint
2090+
- loggercheck
20652091
- maintidx
20662092
- makezero
20672093
- maligned

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +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/logrlint v0.1.0
98+
github.com/timonwong/loggercheck v0.9.3
9999
github.com/tomarrell/wrapcheck/v2 v2.6.2
100100
github.com/tommy-muehle/go-mnd/v2 v2.5.0
101101
github.com/ultraware/funlen v0.0.3

go.sum

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+20
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ var defaultLintersSettings = LintersSettings{
6969
LineLength: 120,
7070
TabWidth: 1,
7171
},
72+
LoggerCheck: LoggerCheckSettings{
73+
Kitlog: true,
74+
Klog: true,
75+
Logr: true,
76+
Zap: true,
77+
RequireStringKey: false,
78+
NoPrintfLike: false,
79+
Rules: nil,
80+
},
7281
MaintIdx: MaintIdxSettings{
7382
Under: 20,
7483
},
@@ -157,6 +166,7 @@ type LintersSettings struct {
157166
InterfaceBloat InterfaceBloatSettings
158167
Ireturn IreturnSettings
159168
Lll LllSettings
169+
LoggerCheck LoggerCheckSettings
160170
MaintIdx MaintIdxSettings
161171
Makezero MakezeroSettings
162172
Maligned MalignedSettings
@@ -479,6 +489,16 @@ type LllSettings struct {
479489
TabWidth int `mapstructure:"tab-width"`
480490
}
481491

492+
type LoggerCheckSettings struct {
493+
Kitlog bool `mapstructure:"kitlog"`
494+
Klog bool `mapstructure:"klog"`
495+
Logr bool `mapstructure:"logr"`
496+
Zap bool `mapstructure:"zap"`
497+
RequireStringKey bool `mapstructure:"require-string-key"`
498+
NoPrintfLike bool `mapstructure:"no-printf-like"`
499+
Rules []string `mapstructure:"rules"`
500+
}
501+
482502
type MaintIdxSettings struct {
483503
Under int `mapstructure:"under"`
484504
}

pkg/golinters/loggercheck.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package golinters
2+
3+
import (
4+
"github.com/timonwong/loggercheck"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewLoggerCheck(settings *config.LoggerCheckSettings) *goanalysis.Linter {
12+
var opts []loggercheck.Option
13+
14+
if settings != nil {
15+
var disable []string
16+
if !settings.Kitlog {
17+
disable = append(disable, "kitlog")
18+
}
19+
if !settings.Klog {
20+
disable = append(disable, "klog")
21+
}
22+
if !settings.Logr {
23+
disable = append(disable, "logr")
24+
}
25+
if !settings.Zap {
26+
disable = append(disable, "zap")
27+
}
28+
29+
opts = []loggercheck.Option{
30+
loggercheck.WithDisable(disable),
31+
loggercheck.WithRequireStringKey(settings.RequireStringKey),
32+
loggercheck.WithRules(settings.Rules),
33+
loggercheck.WithNoPrintfLike(settings.NoPrintfLike),
34+
}
35+
}
36+
37+
analyzer := loggercheck.NewAnalyzer(opts...)
38+
return goanalysis.NewLinter(
39+
analyzer.Name,
40+
analyzer.Doc,
41+
[]*analysis.Analyzer{analyzer},
42+
nil,
43+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
44+
}

pkg/golinters/logrlint.go

-19
This file was deleted.

pkg/lint/lintersdb/manager.go

+6-3
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,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
583585
WithSince("v1.8.0").
584586
WithPresets(linter.PresetStyle),
585587

586-
linter.NewConfig(golinters.NewLogrLint()).
588+
linter.NewConfig(golinters.NewLoggerCheck(loggerCheckCfg)).
587589
WithSince("v1.49.0").
588590
WithLoadForGoAnalysis().
589-
WithPresets(linter.PresetBugs).
590-
WithURL("https://github.com/timonwong/logrlint"),
591+
WithPresets(linter.PresetStyle, linter.PresetBugs).
592+
WithAlternativeNames("logrlint").
593+
WithURL("https://github.com/timonwong/loggercheck"),
591594

592595
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
593596
WithSince("v1.44.0").

test/linters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestTypecheck(t *testing.T) {
2929

3030
func TestSourcesFromTestdataSubDir(t *testing.T) {
3131
subDirs := []string{
32-
"logrlint",
32+
"loggercheck",
3333
}
3434

3535
for _, dir := range subDirs {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
linters-settings:
2+
loggercheck:
3+
rules:
4+
- (*command-line-arguments.Logger).Debugw
5+
- (*command-line-arguments.Logger).Infow
6+
- (*command-line-arguments.Logger).Warnw
7+
- (*command-line-arguments.Logger).Errorw
8+
- (*command-line-arguments.Logger).With
9+
- command-line-arguments.Debugw
10+
- command-line-arguments.Infow
11+
- command-line-arguments.Warnw
12+
- command-line-arguments.Errorw
13+
- command-line-arguments.With
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
loggercheck:
3+
kitlog: true
4+
klog: false
5+
logr: false
6+
zap: false
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
loggercheck:
3+
no-printf-like: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
loggercheck:
3+
require-string-key: true
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: true

test/testdata/loggercheck/go.mod

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module loggercheck
2+
3+
go 1.19
4+
5+
require (
6+
github.com/go-kit/log v0.2.1
7+
github.com/go-logr/logr v1.2.3
8+
go.uber.org/zap v1.23.0
9+
k8s.io/klog/v2 v2.70.1
10+
)
11+
12+
require (
13+
github.com/go-logfmt/logfmt v0.5.1 // indirect
14+
go.uber.org/atomic v1.10.0 // indirect
15+
go.uber.org/multierr v1.8.0 // indirect
16+
)

test/testdata/loggercheck/go.sum

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)