Skip to content

Commit

Permalink
feat: rename logrlint to loggercheck
Browse files Browse the repository at this point in the history
Signed-off-by: Timon Wong <timon86.wang@gmail.com>
  • Loading branch information
timonwong committed Aug 26, 2022
1 parent bddc63a commit 6fc71f1
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 9 deletions.
9 changes: 7 additions & 2 deletions .golangci.reference.yml
Expand Up @@ -1111,6 +1111,11 @@ linters-settings:
# Default: 1
tab-width: 1

loggercheck:
logr: true
klog: true
zap: true

maintidx:
# Show functions with maintainability index lower than N.
# A high index indicates better maintainability (it's kind of the opposite of complexity).
Expand Down Expand Up @@ -1939,7 +1944,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- loggercheck
- maintidx
- makezero
- maligned
Expand Down Expand Up @@ -2044,7 +2049,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- loggercheck
- maintidx
- makezero
- maligned
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -95,7 +95,8 @@ require (
github.com/tdakkota/asciicheck v0.1.1
github.com/tetafro/godot v1.4.11
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
github.com/timonwong/logrlint v0.1.0
github.com/timonwong/loggercheck v0.5.0
github.com/timonwong/logrlint v0.1.1
github.com/tomarrell/wrapcheck/v2 v2.6.2
github.com/tommy-muehle/go-mnd/v2 v2.5.0
github.com/ultraware/funlen v0.0.3
Expand Down
6 changes: 4 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -66,6 +66,11 @@ var defaultLintersSettings = LintersSettings{
LineLength: 120,
TabWidth: 1,
},
LoggerCheck: LoggerCheckSettings{
Logr: true,
Klog: true,
Zap: true,
},
MaintIdx: MaintIdxSettings{
Under: 20,
},
Expand Down Expand Up @@ -154,6 +159,7 @@ type LintersSettings struct {
InterfaceBloat InterfaceBloatSettings
Ireturn IreturnSettings
Lll LllSettings
LoggerCheck LoggerCheckSettings
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Maligned MalignedSettings
Expand Down Expand Up @@ -470,6 +476,12 @@ type LllSettings struct {
TabWidth int `mapstructure:"tab-width"`
}

type LoggerCheckSettings struct {
Logr bool `mapstructure:"logr"`
Klog bool `mapstructure:"klog"`
Zap bool `mapstructure:"zap"`
}

type MaintIdxSettings struct {
Under int `mapstructure:"under"`
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/golinters/loggercheck.go
@@ -0,0 +1,39 @@
package golinters

import (
"strings"

"github.com/timonwong/loggercheck"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewLoggerCheck(settings *config.LoggerCheckSettings) *goanalysis.Linter {
analyzer := loggercheck.NewAnalyzer()
cfg := map[string]map[string]interface{}{}
if settings != nil {
var disabled []string
if !settings.Logr {
disabled = append(disabled, "logr")
}
if !settings.Klog {
disabled = append(disabled, "klog")
}
if !settings.Logr {
disabled = append(disabled, "zap")
}
linterCfg := map[string]interface{}{
"disable": strings.Join(disabled, ","),
}
cfg[analyzer.Name] = linterCfg
}

return goanalysis.NewLinter(
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{analyzer},
cfg,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
12 changes: 11 additions & 1 deletion pkg/lint/lintersdb/manager.go
Expand Up @@ -140,6 +140,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
interfaceBloatCfg *config.InterfaceBloatSettings
ireturnCfg *config.IreturnSettings
lllCfg *config.LllSettings
loggerCheckCfg *config.LoggerCheckSettings
maintIdxCfg *config.MaintIdxSettings
makezeroCfg *config.MakezeroSettings
malignedCfg *config.MalignedSettings
Expand Down Expand Up @@ -214,6 +215,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
interfaceBloatCfg = &m.cfg.LintersSettings.InterfaceBloat
ireturnCfg = &m.cfg.LintersSettings.Ireturn
lllCfg = &m.cfg.LintersSettings.Lll
loggerCheckCfg = &m.cfg.LintersSettings.LoggerCheck
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
makezeroCfg = &m.cfg.LintersSettings.Makezero
malignedCfg = &m.cfg.LintersSettings.Maligned
Expand Down Expand Up @@ -583,11 +585,19 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.8.0").
WithPresets(linter.PresetStyle),

linter.NewConfig(golinters.NewLoggerCheck(loggerCheckCfg)).
WithSince("v1.50.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithAlternativeNames("logrlint").
WithURL("https://github.com/timonwong/loggercheck"),

linter.NewConfig(golinters.NewLogrLint()).
WithSince("v1.49.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/timonwong/logrlint"),
WithURL("https://github.com/timonwong/logrlint").
Deprecated("The repository of the linter has been archived by the owner.", "v1.50.0", "loggercheck"),

linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.44.0").
Expand Down
3 changes: 2 additions & 1 deletion test/linters_test.go
Expand Up @@ -29,7 +29,8 @@ func TestTypecheck(t *testing.T) {

func TestSourcesFromTestdataSubDir(t *testing.T) {
subDirs := []string{
"logrlint",
"loggercheck",
"logrlint", // deprecated linter
}

for _, dir := range subDirs {
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/loggercheck/configs/loggercheck_logronly.yml
@@ -0,0 +1,5 @@
linters-settings:
loggercheck:
logr: true
klog: false
zap: false
5 changes: 5 additions & 0 deletions test/testdata/loggercheck/configs/loggercheck_zaponly.yml
@@ -0,0 +1,5 @@
linters-settings:
loggercheck:
logr: false
klog: false
zap: zap
11 changes: 11 additions & 0 deletions test/testdata/loggercheck/go.mod
@@ -0,0 +1,11 @@
module loggercheck

go 1.16

require (
github.com/go-logr/logr v1.2.3
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0
k8s.io/klog/v2 v2.70.1
)
66 changes: 66 additions & 0 deletions test/testdata/loggercheck/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions test/testdata/loggercheck/loggercheck_all.go
@@ -0,0 +1,24 @@
//golangcitest:args -Eloggercheck
package loggercheck

import (
"fmt"

"github.com/go-logr/logr"
"go.uber.org/zap"
"k8s.io/klog/v2"
)

func ExampleAll() {
log := logr.Discard()
log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging`
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")

klog.InfoS("message", "key1") // want `odd number of arguments passed as key-value pairs for logging`

sugar := zap.NewExample().Sugar()
defer sugar.Sync()
sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
}
15 changes: 15 additions & 0 deletions test/testdata/loggercheck/loggercheck_logronly.go
@@ -0,0 +1,15 @@
//golangcitest:args -Eloggercheck
//golangcitest:config_path configs/loggercheck_logronly.yml
package loggercheck

import (
"github.com/go-logr/logr"
"k8s.io/klog/v2"
)

func ExampleLogrOnly() {
log := logr.Discard()
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`

klog.InfoS("message", "key1")
}
13 changes: 13 additions & 0 deletions test/testdata/loggercheck/loggercheck_zaponly.go
@@ -0,0 +1,13 @@
//golangcitest:args -Eloggercheck
//golangcitest:config_path configs/loggercheck_zaponly.yml
package loggercheck

import "go.uber.org/zap"

func ExampleZapOnly() {
sugar := zap.NewExample().Sugar()
defer sugar.Sync()

sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
sugar.Errorw("error message", "key1") // want `odd number of arguments passed as key-value pairs for logging`
}
2 changes: 1 addition & 1 deletion test/testdata/logrlint/go.mod
Expand Up @@ -2,4 +2,4 @@ module logrlint

go 1.16

require github.com/go-logr/logr v1.2.3
require github.com/go-logr/logr v1.2.3
2 changes: 1 addition & 1 deletion test/testdata/logrlint/logrlint.go
@@ -1,5 +1,5 @@
//golangcitest:args -Elogrlint
package logrlint
package loggercheck

import (
"fmt"
Expand Down

0 comments on commit 6fc71f1

Please sign in to comment.