Skip to content

Commit 33c140e

Browse files
authored
loggercheck: add missing slog option (#5155)
1 parent e378878 commit 33c140e

12 files changed

+50
-0
lines changed

.golangci.next.reference.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,9 @@ linters-settings:
20322032
# Allow check for the github.com/go-logr/logr library.
20332033
# Default: true
20342034
logr: false
2035+
# Allow check for the log/slog library.
2036+
# Default: true
2037+
slog: false
20352038
# Allow check for the "sugar logger" from go.uber.org/zap library.
20362039
# Default: true
20372040
zap: false

.golangci.reference.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,9 @@ linters-settings:
20322032
# Allow check for the github.com/go-logr/logr library.
20332033
# Default: true
20342034
logr: false
2035+
# Allow check for the log/slog library.
2036+
# Default: true
2037+
slog: false
20352038
# Allow check for the "sugar logger" from go.uber.org/zap library.
20362039
# Default: true
20372040
zap: false

jsonschema/golangci.jsonschema.json

+5
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,11 @@
21112111
"type": "boolean",
21122112
"default": true
21132113
},
2114+
"slog": {
2115+
"description": "Allow check for the log/slog library.",
2116+
"type": "boolean",
2117+
"default": true
2118+
},
21142119
"zap": {
21152120
"description": "Allow check for the \"sugar logger\" from go.uber.org/zap library.",
21162121
"type": "boolean",

jsonschema/golangci.next.jsonschema.json

+5
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,11 @@
21112111
"type": "boolean",
21122112
"default": true
21132113
},
2114+
"slog": {
2115+
"description": "Allow check for the log/slog library.",
2116+
"type": "boolean",
2117+
"default": true
2118+
},
21142119
"zap": {
21152120
"description": "Allow check for the \"sugar logger\" from go.uber.org/zap library.",
21162121
"type": "boolean",

pkg/config/linters_settings.go

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ var defaultLintersSettings = LintersSettings{
105105
Kitlog: true,
106106
Klog: true,
107107
Logr: true,
108+
Slog: true,
108109
Zap: true,
109110
RequireStringKey: false,
110111
NoPrintfLike: false,
@@ -687,6 +688,7 @@ type LoggerCheckSettings struct {
687688
Kitlog bool `mapstructure:"kitlog"`
688689
Klog bool `mapstructure:"klog"`
689690
Logr bool `mapstructure:"logr"`
691+
Slog bool `mapstructure:"slog"`
690692
Zap bool `mapstructure:"zap"`
691693
RequireStringKey bool `mapstructure:"require-string-key"`
692694
NoPrintfLike bool `mapstructure:"no-printf-like"`

pkg/golinters/loggercheck/loggercheck.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func New(settings *config.LoggerCheckSettings) *goanalysis.Linter {
2222
if !settings.Logr {
2323
disable = append(disable, "logr")
2424
}
25+
if !settings.Slog {
26+
disable = append(disable, "slog")
27+
}
2528
if !settings.Zap {
2629
disable = append(disable, "zap")
2730
}

pkg/golinters/loggercheck/testdata/loggercheck_default.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package loggercheck
33

44
import (
55
"fmt"
6+
"log/slog"
67

78
"github.com/go-logr/logr"
89
"go.uber.org/zap"
@@ -26,3 +27,10 @@ func ExampleZapSugarNotChecked() {
2627
defer sugar.Sync()
2728
sugar.Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
2829
}
30+
31+
func ExampleSlog() {
32+
logger := slog.With("key1", "value1")
33+
34+
logger.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
35+
slog.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
36+
}

pkg/golinters/loggercheck/testdata/loggercheck_kitlogonly.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ linters-settings:
33
kitlog: true
44
klog: false
55
logr: false
6+
slog: false
67
zap: false

pkg/golinters/loggercheck/testdata/loggercheck_logronly.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ linters-settings:
22
loggercheck:
33
logr: true
44
klog: false
5+
slog: false
56
zap: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//golangcitest:args -Eloggercheck
2+
//golangcitest:config_path testdata/loggercheck_slogonly.yml
3+
package loggercheck
4+
5+
import "log/slog"
6+
7+
func ExampleSlogOnly() {
8+
logger := slog.With("key1", "value1")
9+
10+
logger.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
11+
slog.Info("msg", "key1") // want `odd number of arguments passed as key-value pairs for logging`
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
loggercheck:
3+
logr: false
4+
klog: false
5+
slog: true
6+
zap: false

pkg/golinters/loggercheck/testdata/loggercheck_zaponly.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ linters-settings:
22
loggercheck:
33
logr: false
44
klog: false
5+
slog: false
56
zap: true

0 commit comments

Comments
 (0)