diff --git a/logrlint_test.go b/logrlint_test.go index 615fa8b..89111f3 100644 --- a/logrlint_test.go +++ b/logrlint_test.go @@ -45,8 +45,17 @@ func TestKlogOnly(t *testing.T) { func TestCustomOnly(t *testing.T) { testdata := analysistest.TestData() - customLoggerFlag := "-logger=mylogger:a/customonly:(*a/customonly.Logger).Debugw,(*a/customonly.Logger).Infow," + - "(*a/customonly.Logger).Warnw,(*a/customonly.Logger).Errorw,(*a/customonly.Logger).With" + customLoggerFlag := "-logger=mylogger:a/customonly:" + + "(*a/customonly.Logger).Debugw," + + "(*a/customonly.Logger).Infow," + + "(*a/customonly.Logger).Warnw," + + "(*a/customonly.Logger).Errorw," + + "(*a/customonly.Logger).With," + + "a/customonly.Debugw," + + "a/customonly.Infow," + + "a/customonly.Warnw," + + "a/customonly.Errorw," + + "a/customonly.With" testCases := []struct { name string @@ -83,6 +92,12 @@ func TestOptions(t *testing.T) { "(*a/customonly.Logger).Warnw", "(*a/customonly.Logger).Errorw", "(*a/customonly.Logger).With", + + "a/customonly.Debugw", + "a/customonly.Infow", + "a/customonly.Warnw", + "a/customonly.Errorw", + "a/customonly.With", }) testCases := []struct { diff --git a/testdata/src/a/customonly/example.go b/testdata/src/a/customonly/example.go index 4c4cbba..b4b497a 100644 --- a/testdata/src/a/customonly/example.go +++ b/testdata/src/a/customonly/example.go @@ -35,3 +35,20 @@ func ExampleCustomLogger() { log.With("with_key1", "with_value1").Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging` log.With("with_key1").Infow("message", "key1", "value1") // want `odd number of arguments passed as key-value pairs for logging` } + +func ExampleCustomLoggerPackageLevelFunc() { + err := errors.New("example error") + + defer Sync() + + Infow("abc", "key1", "value1") + Infow("abc", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging` + + Errorw("message", "err", err, "key1", "value1") + Errorw("message", err, "key1", "value1", "key2", "value2") // want `odd number of arguments passed as key-value pairs for logging` + + // with test + With("with_key1", "with_value1").Infow("message", "key1", "value1") + With("with_key1", "with_value1").Infow("message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging` + With("with_key1").Infow("message", "key1", "value1") // want `odd number of arguments passed as key-value pairs for logging` +} diff --git a/testdata/src/a/customonly/zap_wrapper.go b/testdata/src/a/customonly/zap_wrapper.go index f3f674f..f1eaca6 100644 --- a/testdata/src/a/customonly/zap_wrapper.go +++ b/testdata/src/a/customonly/zap_wrapper.go @@ -2,6 +2,8 @@ package customonly import "go.uber.org/zap" +var l = New() + type Logger struct { s *zap.SugaredLogger } @@ -40,3 +42,35 @@ func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) { func (l *Logger) Sync() error { return l.s.Sync() } + +// package level wrap func + +func With(keysAndValues ...interface{}) *Logger { + return &Logger{ + s: l.s.With(keysAndValues...), + } +} + +func Debugw(msg string, keysAndValues ...interface{}) { + l.s.Debugw(msg, keysAndValues...) +} + +func Infow(msg string, keysAndValues ...interface{}) { + l.s.Infow(msg, keysAndValues...) +} + +func Warnw(msg string, keysAndValues ...interface{}) { + l.s.Warnw(msg, keysAndValues...) +} + +func Errorw(msg string, keysAndValues ...interface{}) { + l.s.Errorw(msg, keysAndValues...) +} + +func Fatalw(msg string, keysAndValues ...interface{}) { + l.s.Fatalw(msg, keysAndValues...) +} + +func Sync() error { + return l.s.Sync() +}