Skip to content

Commit

Permalink
test: add package level custom logger checker test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys3 committed Aug 26, 2022
1 parent c6c798d commit d0e7f33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
19 changes: 17 additions & 2 deletions logrlint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions testdata/src/a/customonly/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
34 changes: 34 additions & 0 deletions testdata/src/a/customonly/zap_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package customonly

import "go.uber.org/zap"

var l = New()

type Logger struct {
s *zap.SugaredLogger
}
Expand Down Expand Up @@ -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()
}

0 comments on commit d0e7f33

Please sign in to comment.