Help us shape the future of Go! Take the 2026 Go Developer Survey

loggercheck

package module
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 16 Imported by: 10

README

loggercheck

Description

A linter checks the odd number of key and value pairs for common logger libraries:

For klog and logr, it also reports pointer values whose element type implements fmt.Stringer (for example *metav1.Time, which embeds time.Time). When such a pointer is nil, klog and logr's funcr render it as <panic: runtime error: invalid memory address or nil pointer dereference>. Wrap the value with klog.SafePtr, check it for nil first, or call .String() when it cannot be nil. Values that are provably non-nil at the call site, such as &T{} or a pointer used after a != nil check or a nil guard that returns or exits, are not reported.

It's recommended to use loggercheck with golangci-lint.

Badges

Build Status Coverage License Release

Install

go install github.com/timonwong/loggercheck/cmd/loggercheck

Usage

loggercheck: Checks key value pairs for common logger libraries (kitlog,klog,logr,slog,zap).

Usage: loggercheck [-flag] [package]


Flags:
  -V    print version and exit
  -all
        no effect (deprecated)
  -c int
        display offending line with this many lines of context (default -1)
  -cpuprofile string
        write CPU profile to this file
  -debug string
        debug flags, any subset of "fpstv"
  -disable value
        comma-separated list of disabled logger checker (kitlog,klog,logr,slog,zap) (default kitlog)
  -fix
        apply all suggested fixes
  -flags
        print analyzer flags in JSON
  -json
        emit JSON output
  -memprofile string
        write memory profile to this file
  -noprintflike
        require printf-like format specifier not present in args
  -requirestringkey
        require all logging keys to be inlined constant strings
  -rulefile string
        path to a file contains a list of rules
  -source
        no effect (deprecated)
  -tags string
        no effect (deprecated)
  -test
        indicates whether test files should be analyzed, too (default true)
  -trace string
        write trace log to this file
  -v    no effect (deprecated)

Example

package a

import (
	"context"
	"fmt"

	"github.com/go-logr/logr"
)

func Example() {
	log := logr.Discard()
	log = log.WithValues("key")
	log.Info("message", "key1", "value1", "key2", "value2", "key3")
	log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2")
	log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")

	var log2 logr.Logger
	log2 = log
	log2.Info("message", "key1")

	log3 := logr.FromContextOrDiscard(context.TODO())
	log3.Error(fmt.Errorf("error"), "message", "key1")
}
a.go:12:23: odd number of arguments passed as key-value pairs for logging
a.go:13:22: odd number of arguments passed as key-value pairs for logging
a.go:14:44: odd number of arguments passed as key-value pairs for logging
a.go:19:23: odd number of arguments passed as key-value pairs for logging
a.go:22:45: odd number of arguments passed as key-value pairs for logging

Documentation

Index

Constants

View Source
const Doc = `Checks key value pairs for common logger libraries (kitlog,klog,logr,slog,zap).`

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(opts ...Option) *analysis.Analyzer

Types

type Option added in v0.6.0

type Option func(*loggercheck)

func WithDisable added in v0.6.0

func WithDisable(disable []string) Option

func WithNoPrintfLike added in v0.9.2

func WithNoPrintfLike(noPrintfLike bool) Option

func WithRequireStringKey added in v0.7.0

func WithRequireStringKey(requireStringKey bool) Option

func WithRules added in v0.6.0

func WithRules(customRules []string) Option

Directories

Path Synopsis
cmd
loggercheck command
internal
nilness
Package nilness answers whether a pointer passed as a variadic logging argument is provably non-nil at the call site, using an SSA form of the package that is built on first use.
Package nilness answers whether a pointer passed as a variadic logging argument is provably non-nil at the call site, using an SSA form of the package that is built on first use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL