Configure discard handler suggestions
maindiscard-handler check is enabled by default and supports autofix. It suggests using slog.DiscardHandler instead of slog.NewJSONHandler(io.Discard, nil) or similar patterns.repository·main·Indexed 42 days ago
https://github.com/go-simpler/sloglintA Go linter designed to enforce consistent code style and best practices when using the log/slog package. It can be used as a standalone CLI tool or as a plugin for golangci-lint. Features include checks for global logger usage, context requirement, static log messages, log key naming conventions, and support for analyzing custom logging functions.
discard-handler check is enabled by default and supports autofix. It suggests using slog.DiscardHandler instead of slog.NewJSONHandler(io.Discard, nil) or similar patterns.The recommended way to use sloglint is as a plugin for golangci-lint. To enable it, add sloglint to the enable list in your .golangci.yaml configuration file.
# .golangci.yaml
linters:
enable:
- sloglintEnable static-msg to report dynamic log messages (e.g., those built with fmt.Sprintf) and ensure messages are string literals or constants.
# .golangci.yaml
linters:
settings:
sloglint:
static-msg: trueUse msg-style to enforce a specific casing for log messages. Supported styles are:
lowercased: The first letter must be lowercase.capitalized: The first letter must be uppercase.# .golangci.yaml
linters:
settings:
sloglint:
msg-style: "lowercased" # Or "capitalized".You can enforce specific patterns for log arguments using the following settings:
kv-only: true: Reports use of attributes; suggests using ...any functions instead of ...Attr alternatives.attr-only: true: Reports use of key-value pairs; suggests using ...Attr functions instead of ...any alternatives.args-on-sep-lines: true: Reports when two or more arguments are on the same line.# .golangci.yaml
linters:
settings:
sloglint:
kv-only: true
attr-only: true
args-on-sep-lines: trueControl how log keys are defined and named using these settings:
no-raw-keys: true: Reports the use of string literals as keys (requires constants).allowed-keys: A list of explicitly allowed keys.forbidden-keys: A list of keys that are prohibited (e.g., time, level, msg, source).key-naming-case: Enforces a naming convention. Supported values: snake, kebab, camel, pascal.# .golangci.yaml
linters:
settings:
sloglint:
no-raw-keys: true
allowed-keys:
- user_id
forbidden-keys:
- time
- level
key-naming-case: "snake"Use the context setting to report log/slog functions that are called without a context.Context.
all: Report all functions without context.scope: Report functions without context only if a context exists within the scope of the outermost function.# .golangci.yaml
linters:
settings:
sloglint:
context: "all" # Or "scope".Use the no-global setting to report the use of global loggers. You can choose to report all global logger usage or only the use of slog.Default().
# .golangci.yaml
linters:
settings:
sloglint:
no-global: "all" # Or "default".You can extend sloglint to analyze custom functions by defining them in custom-funcs. For each function, you must provide:
name: The full package-qualified name. For methods, wrap the receiver in parentheses (e.g., (*package.Type).Method).msg-pos: The 0-indexed position of the msg string argument. Use -1 if there is no message.args-pos: The 0-indexed position of the args ...any argument. Use -1 if there are no arguments.# .golangci.yaml
linters:
settings:
sloglint:
custom-funcs:
- name: "(*golang.org/x/exp/slog.Logger).InfoContext"
msg-pos: 1
args-pos: 2The Options struct allows you to customize the behavior of the sloglint analyzer. You can control how global loggers, context usage, message styles, argument types, and log keys are validated.
NoGlobalLogger: Set to `The New function creates a new *analysis.Analyzer instance for use with the Go analysis framework (e.g., via golangci-lint or go vet).
If you pass nil as the opts argument, the analyzer defaults to enabling NoMixedArguments: true. You can provide a pointer to an Options struct to configure specific linting rules such as key naming conventions, allowed/forbidden keys, and message styles.
import "github.com/go-simpler/go-simpler/sloglint"
// Create an analyzer with custom options
opts := &sloglint.Options{
NoMixedArguments: true,
// ... other options
}
analyzer := sloglint.New(opts)