aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/timonwong/loggercheck/staticrules.go
blob: f955b3434117d7437593ce5078fbedc1944fb5f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package loggercheck

import (
	"errors"
	"fmt"

	"github.com/timonwong/loggercheck/internal/checkers"
	"github.com/timonwong/loggercheck/internal/rules"
)

var (
	staticRuleList = []rules.Ruleset{
		mustNewStaticRuleSet("logr", []string{
			"(github.com/go-logr/logr.Logger).Error",
			"(github.com/go-logr/logr.Logger).Info",
			"(github.com/go-logr/logr.Logger).WithValues",
		}),
		mustNewStaticRuleSet("klog", []string{
			"k8s.io/klog/v2.InfoS",
			"k8s.io/klog/v2.InfoSDepth",
			"k8s.io/klog/v2.ErrorS",
			"(k8s.io/klog/v2.Verbose).InfoS",
			"(k8s.io/klog/v2.Verbose).InfoSDepth",
			"(k8s.io/klog/v2.Verbose).ErrorS",
		}),
		mustNewStaticRuleSet("zap", []string{
			"(*go.uber.org/zap.SugaredLogger).With",
			"(*go.uber.org/zap.SugaredLogger).Debugw",
			"(*go.uber.org/zap.SugaredLogger).Infow",
			"(*go.uber.org/zap.SugaredLogger).Warnw",
			"(*go.uber.org/zap.SugaredLogger).Errorw",
			"(*go.uber.org/zap.SugaredLogger).DPanicw",
			"(*go.uber.org/zap.SugaredLogger).Panicw",
			"(*go.uber.org/zap.SugaredLogger).Fatalw",
		}),
		mustNewStaticRuleSet("kitlog", []string{
			"github.com/go-kit/log.With",
			"github.com/go-kit/log.WithPrefix",
			"github.com/go-kit/log.WithSuffix",
			"(github.com/go-kit/log.Logger).Log",
		}),
	}
	checkerByRulesetName = map[string]checkers.Checker{
		// by default, checkers.General will be used.
		"zap": checkers.Zap{},
	}
)

// mustNewStaticRuleSet only called at init, catch errors during development.
// In production it will not panic.
func mustNewStaticRuleSet(name string, lines []string) rules.Ruleset {
	if len(lines) == 0 {
		panic(errors.New("no rules provided"))
	}

	rulesetList, err := rules.ParseRules(lines)
	if err != nil {
		panic(err)
	}

	if len(rulesetList) != 1 {
		panic(fmt.Errorf("expected 1 ruleset, got %d", len(rulesetList)))
	}

	ruleset := rulesetList[0]
	ruleset.Name = name
	return ruleset
}