Skip to content

loggercheck: add missing slog option #5155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading