Skip to content

Commit

Permalink
feat: rename logrlint to loggercheck (golangci#3144)
Browse files Browse the repository at this point in the history
  • Loading branch information
timonwong authored and SeigeC committed Apr 4, 2023
1 parent 74e77b8 commit ec24632
Show file tree
Hide file tree
Showing 26 changed files with 454 additions and 37 deletions.
30 changes: 28 additions & 2 deletions .golangci.reference.yml
Expand Up @@ -1144,6 +1144,32 @@ linters-settings:
# Default: 1
tab-width: 1

loggercheck:
# Allow check for the github.com/go-kit/log library.
# Default: true
kitlog: false
# Allow check for the k8s.io/klog/v2 library.
# Default: true
klog: false
# Allow check for the github.com/go-logr/logr library.
# Default: true
logr: false
# Allow check for the "sugar logger" from go.uber.org/zap library.
# Default: true
zap: false
# Require all logging keys to be inlined constant strings.
# Default: false
require-string-key: true
# Require printf-like format specifier (%s, %d for example) not present.
# Default: false
no-printf-like: true
# List of custom rules to check against, where each rule is a single logger pattern, useful for wrapped loggers.
# For example: https://github.com/timonwong/loggercheck/blob/7395ab86595781e33f7afba27ad7b55e6956ebcd/testdata/custom-rules.txt
# Default: empty
rules:
- k8s.io/klog/v2.InfoS # package level exported functions
- (github.com/go-logr/logr.Logger).Error # "Methods"
- (*go.uber.org/zap.SugaredLogger).With # Also "Methods", but with a pointer receiver
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 @@ -1981,7 +2007,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- loggercheck
- maintidx
- makezero
- maligned
Expand Down Expand Up @@ -2086,7 +2112,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- loggercheck
- maintidx
- makezero
- maligned
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -95,7 +95,7 @@ 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.9.3
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
8 changes: 6 additions & 2 deletions go.sum

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

20 changes: 20 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -69,6 +69,15 @@ var defaultLintersSettings = LintersSettings{
LineLength: 120,
TabWidth: 1,
},
LoggerCheck: LoggerCheckSettings{
Kitlog: true,
Klog: true,
Logr: true,
Zap: true,
RequireStringKey: false,
NoPrintfLike: false,
Rules: nil,
},
MaintIdx: MaintIdxSettings{
Under: 20,
},
Expand Down Expand Up @@ -157,6 +166,7 @@ type LintersSettings struct {
InterfaceBloat InterfaceBloatSettings
Ireturn IreturnSettings
Lll LllSettings
LoggerCheck LoggerCheckSettings
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Maligned MalignedSettings
Expand Down Expand Up @@ -479,6 +489,16 @@ type LllSettings struct {
TabWidth int `mapstructure:"tab-width"`
}

type LoggerCheckSettings struct {
Kitlog bool `mapstructure:"kitlog"`
Klog bool `mapstructure:"klog"`
Logr bool `mapstructure:"logr"`
Zap bool `mapstructure:"zap"`
RequireStringKey bool `mapstructure:"require-string-key"`
NoPrintfLike bool `mapstructure:"no-printf-like"`
Rules []string `mapstructure:"rules"`
}

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

import (
"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 {
var opts []loggercheck.Option

if settings != nil {
var disable []string
if !settings.Kitlog {
disable = append(disable, "kitlog")
}
if !settings.Klog {
disable = append(disable, "klog")
}
if !settings.Logr {
disable = append(disable, "logr")
}
if !settings.Zap {
disable = append(disable, "zap")
}

opts = []loggercheck.Option{
loggercheck.WithDisable(disable),
loggercheck.WithRequireStringKey(settings.RequireStringKey),
loggercheck.WithRules(settings.Rules),
loggercheck.WithNoPrintfLike(settings.NoPrintfLike),
}
}

analyzer := loggercheck.NewAnalyzer(opts...)
return goanalysis.NewLinter(
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
19 changes: 0 additions & 19 deletions pkg/golinters/logrlint.go

This file was deleted.

9 changes: 6 additions & 3 deletions 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,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.8.0").
WithPresets(linter.PresetStyle),

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

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

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

for _, dir := range subDirs {
Expand Down
13 changes: 13 additions & 0 deletions test/testdata/loggercheck/configs/loggercheck_custom.yml
@@ -0,0 +1,13 @@
linters-settings:
loggercheck:
rules:
- (*command-line-arguments.Logger).Debugw
- (*command-line-arguments.Logger).Infow
- (*command-line-arguments.Logger).Warnw
- (*command-line-arguments.Logger).Errorw
- (*command-line-arguments.Logger).With
- command-line-arguments.Debugw
- command-line-arguments.Infow
- command-line-arguments.Warnw
- command-line-arguments.Errorw
- command-line-arguments.With
6 changes: 6 additions & 0 deletions test/testdata/loggercheck/configs/loggercheck_kitlogonly.yml
@@ -0,0 +1,6 @@
linters-settings:
loggercheck:
kitlog: true
klog: false
logr: false
zap: false
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
@@ -0,0 +1,3 @@
linters-settings:
loggercheck:
no-printf-like: true
@@ -0,0 +1,3 @@
linters-settings:
loggercheck:
require-string-key: true
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: true
16 changes: 16 additions & 0 deletions test/testdata/loggercheck/go.mod
@@ -0,0 +1,16 @@
module loggercheck

go 1.19

require (
github.com/go-kit/log v0.2.1
github.com/go-logr/logr v1.2.3
go.uber.org/zap v1.23.0
k8s.io/klog/v2 v2.70.1
)

require (
github.com/go-logfmt/logfmt v0.5.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
)
32 changes: 32 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.

0 comments on commit ec24632

Please sign in to comment.