Skip to content

Commit

Permalink
chore: add more tests for logx/logc (zeromicro#4603)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jan 25, 2025
1 parent 64e8c94 commit a32f6d7
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 68 deletions.
38 changes: 28 additions & 10 deletions core/logc/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ func Debugf(ctx context.Context, format string, v ...interface{}) {
getLogger(ctx).Debugf(format, v...)
}

// Debugfn writes fn result into access log.
// This is useful when the function is expensive to compute,
// and we want to log it only when necessary.
func Debugfn(ctx context.Context, fn func() any) {
getLogger(ctx).Debugfn(fn)
}

// Debugv writes v into access log with json content.
func Debugv(ctx context.Context, v interface{}) {
getLogger(ctx).Debugv(v)
}

// Debugfn writes fn result into access log.
func Debugfn(ctx context.Context, fn func() string) {
getLogger(ctx).Debugfn(fn)
}

// Debugw writes msg along with fields into the access log.
func Debugw(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Debugw(msg, fields...)
Expand All @@ -62,6 +64,13 @@ func Errorf(ctx context.Context, format string, v ...any) {
getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
}

// Errorfn writes fn result into error log.
// This is useful when the function is expensive to compute,
// and we want to log it only when necessary.
func Errorfn(ctx context.Context, fn func() any) {
getLogger(ctx).Errorfn(fn)
}

// Errorv writes v into error log with json content.
// No call stack attached, because not elegant to pack the messages.
func Errorv(ctx context.Context, v any) {
Expand All @@ -88,16 +97,18 @@ func Infof(ctx context.Context, format string, v ...any) {
getLogger(ctx).Infof(format, v...)
}

// Infofn writes fn result into access log.
// This is useful when the function is expensive to compute,
// and we want to log it only when necessary.
func Infofn(ctx context.Context, fn func() any) {
getLogger(ctx).Infofn(fn)
}

// Infov writes v into access log with json content.
func Infov(ctx context.Context, v any) {
getLogger(ctx).Infov(v)
}

// Infofn writes fn result into access log.
func Infofn(ctx context.Context, fn func() string) {
getLogger(ctx).Infofn(fn)
}

// Infow writes msg along with fields into the access log.
func Infow(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Infow(msg, fields...)
Expand Down Expand Up @@ -137,6 +148,13 @@ func Slowf(ctx context.Context, format string, v ...any) {
getLogger(ctx).Slowf(format, v...)
}

// Slowfn writes fn result into slow log.
// This is useful when the function is expensive to compute,
// and we want to log it only when necessary.
func Slowfn(ctx context.Context, fn func() any) {
getLogger(ctx).Slowfn(fn)
}

// Slowv writes v into slow log with json content.
func Slowv(ctx context.Context, v any) {
getLogger(ctx).Slowv(v)
Expand Down
36 changes: 36 additions & 0 deletions core/logc/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ func TestErrorf(t *testing.T) {
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestErrorfn(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Errorfn(context.Background(), func() any {
return fmt.Sprintf("foo %s", "bar")
})
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestErrorv(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Expand Down Expand Up @@ -77,6 +86,15 @@ func TestInfof(t *testing.T) {
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestInfofn(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Infofn(context.Background(), func() any {
return fmt.Sprintf("foo %s", "bar")
})
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestInfov(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Expand Down Expand Up @@ -105,6 +123,15 @@ func TestDebugf(t *testing.T) {
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestDebugfn(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Debugfn(context.Background(), func() any {
return fmt.Sprintf("foo %s", "bar")
})
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
}

func TestDebugv(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Expand Down Expand Up @@ -148,6 +175,15 @@ func TestSlowf(t *testing.T) {
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
}

func TestSlowfn(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Slowfn(context.Background(), func() any {
return fmt.Sprintf("foo %s", "bar")
})
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
}

func TestSlowv(t *testing.T) {
buf := logtest.NewCollector(t)
file, line := getFileLine()
Expand Down
12 changes: 8 additions & 4 deletions core/logx/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ type Logger interface {
Debug(...any)
// Debugf logs a message at debug level.
Debugf(string, ...any)
// Debugfn logs a message at debug level.
Debugfn(func() any)
// Debugv logs a message at debug level.
Debugv(any)
// Debugfn logs a message at debug level.
Debugfn(func() string)
// Debugw logs a message at debug level.
Debugw(string, ...LogField)
// Error logs a message at error level.
Error(...any)
// Errorf logs a message at error level.
Errorf(string, ...any)
// Errorfn logs a message at error level.
Errorfn(func() any)
// Errorv logs a message at error level.
Errorv(any)
// Errorw logs a message at error level.
Expand All @@ -29,16 +31,18 @@ type Logger interface {
Info(...any)
// Infof logs a message at info level.
Infof(string, ...any)
// Infofn logs a message at info level.
Infofn(func() any)
// Infov logs a message at info level.
Infov(any)
// Infofn logs a message at info level.
Infofn(func() string)
// Infow logs a message at info level.
Infow(string, ...LogField)
// Slow logs a message at slow level.
Slow(...any)
// Slowf logs a message at slow level.
Slowf(string, ...any)
// Slowfn logs a message at slow level.
Slowfn(func() any)
// Slowv logs a message at slow level.
Slowv(any)
// Sloww logs a message at slow level.
Expand Down
41 changes: 29 additions & 12 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ func Debugf(format string, v ...any) {
}
}

// Debugv writes v into access log with json content.
func Debugv(v any) {
// Debugfn writes function result into access log if debug level enabled.
// This is useful when the function is expensive to call and debug level disabled.
func Debugfn(fn func() any) {
if shallLog(DebugLevel) {
writeDebug(v)
writeDebug(fn())
}
}

// Debugfn writes function result into access log.
func Debugfn(fn func() string) {
// Debugv writes v into access log with json content.
func Debugv(v any) {
if shallLog(DebugLevel) {
writeDebug(fn())
writeDebug(v)
}
}

Expand Down Expand Up @@ -146,6 +147,13 @@ func Errorf(format string, v ...any) {
}
}

// Errorfn writes function result into error log.
func Errorfn(fn func() any) {
if shallLog(ErrorLevel) {
writeError(fn())
}
}

// ErrorStack writes v along with call stack into error log.
func ErrorStack(v ...any) {
if shallLog(ErrorLevel) {
Expand Down Expand Up @@ -229,17 +237,18 @@ func Infof(format string, v ...any) {
}
}

// Infov writes v into access log with json content.
func Infov(v any) {
// Infofn writes function result into access log.
// This is useful when the function is expensive to call and info level disabled.
func Infofn(fn func() any) {
if shallLog(InfoLevel) {
writeInfo(v)
writeInfo(fn())
}
}

// Infofn writes function result into access log.
func Infofn(fn func() string) {
// Infov writes v into access log with json content.
func Infov(v any) {
if shallLog(InfoLevel) {
writeInfo(fn())
writeInfo(v)
}
}

Expand Down Expand Up @@ -362,6 +371,14 @@ func Slowf(format string, v ...any) {
}
}

// Slowfn writes function result into slow log.
// This is useful when the function is expensive to call and slow level disabled.
func Slowfn(fn func() any) {
if shallLog(ErrorLevel) {
writeSlow(fn())
}
}

// Slowv writes v into slow log with json content.
func Slowv(v any) {
if shallLog(ErrorLevel) {
Expand Down
Loading

0 comments on commit a32f6d7

Please sign in to comment.