Skip to content

Commit

Permalink
loggercheck: add missing slog option (#5155)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Nov 25, 2024
1 parent e378878 commit 33c140e
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,9 @@ linters-settings:
# Allow check for the github.com/go-logr/logr library.
# Default: true
logr: false
# Allow check for the log/slog library.
# Default: true
slog: false
# Allow check for the "sugar logger" from go.uber.org/zap library.
# Default: true
zap: false
Expand Down
3 changes: 3 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,9 @@ linters-settings:
# Allow check for the github.com/go-logr/logr library.
# Default: true
logr: false
# Allow check for the log/slog library.
# Default: true
slog: false
# Allow check for the "sugar logger" from go.uber.org/zap library.
# Default: true
zap: false
Expand Down
5 changes: 5 additions & 0 deletions jsonschema/golangci.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,11 @@
"type": "boolean",
"default": true
},
"slog": {
"description": "Allow check for the log/slog library.",
"type": "boolean",
"default": true
},
"zap": {
"description": "Allow check for the \"sugar logger\" from go.uber.org/zap library.",
"type": "boolean",
Expand Down
5 changes: 5 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,11 @@
"type": "boolean",
"default": true
},
"slog": {
"description": "Allow check for the log/slog library.",
"type": "boolean",
"default": true
},
"zap": {
"description": "Allow check for the \"sugar logger\" from go.uber.org/zap library.",
"type": "boolean",
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var defaultLintersSettings = LintersSettings{
Kitlog: true,
Klog: true,
Logr: true,
Slog: true,
Zap: true,
RequireStringKey: false,
NoPrintfLike: false,
Expand Down Expand Up @@ -687,6 +688,7 @@ type LoggerCheckSettings struct {
Kitlog bool `mapstructure:"kitlog"`
Klog bool `mapstructure:"klog"`
Logr bool `mapstructure:"logr"`
Slog bool `mapstructure:"slog"`
Zap bool `mapstructure:"zap"`
RequireStringKey bool `mapstructure:"require-string-key"`
NoPrintfLike bool `mapstructure:"no-printf-like"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/golinters/loggercheck/loggercheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func New(settings *config.LoggerCheckSettings) *goanalysis.Linter {
if !settings.Logr {
disable = append(disable, "logr")
}
if !settings.Slog {
disable = append(disable, "slog")
}
if !settings.Zap {
disable = append(disable, "zap")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/golinters/loggercheck/testdata/loggercheck_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package loggercheck

import (
"fmt"
"log/slog"

"github.com/go-logr/logr"
"go.uber.org/zap"
Expand All @@ -26,3 +27,10 @@ func ExampleZapSugarNotChecked() {
defer sugar.Sync()
sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
}

func ExampleSlog() {
logger := slog.With("key1", "value1")

logger.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
slog.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ linters-settings:
kitlog: true
klog: false
logr: false
slog: false
zap: false
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ linters-settings:
loggercheck:
logr: true
klog: false
slog: false
zap: false
12 changes: 12 additions & 0 deletions pkg/golinters/loggercheck/testdata/loggercheck_slogonly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//golangcitest:args -Eloggercheck
//golangcitest:config_path testdata/loggercheck_slogonly.yml
package loggercheck

import "log/slog"

func ExampleSlogOnly() {
logger := slog.With("key1", "value1")

logger.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
slog.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
}
6 changes: 6 additions & 0 deletions pkg/golinters/loggercheck/testdata/loggercheck_slogonly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
loggercheck:
logr: false
klog: false
slog: true
zap: false
1 change: 1 addition & 0 deletions pkg/golinters/loggercheck/testdata/loggercheck_zaponly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ linters-settings:
loggercheck:
logr: false
klog: false
slog: false
zap: true

0 comments on commit 33c140e

Please sign in to comment.