Skip to content

Commit

Permalink
add Debugfn and Infofn to logx/logc #4595 (#4598)
Browse files Browse the repository at this point in the history
  • Loading branch information
studyzy authored Jan 25, 2025
1 parent 7d05a4b commit 64e8c94
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
10 changes: 10 additions & 0 deletions core/logc/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ 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 Down Expand Up @@ -88,6 +93,11 @@ 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
4 changes: 4 additions & 0 deletions core/logx/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Logger interface {
Debugf(string, ...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.
Expand All @@ -29,6 +31,8 @@ type Logger interface {
Infof(string, ...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.
Expand Down
14 changes: 14 additions & 0 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func Debugv(v any) {
}
}

// Debugfn writes function result into access log.
func Debugfn(fn func() string) {
if shallLog(DebugLevel) {
writeDebug(fn())
}
}

// Debugw writes msg along with fields into the access log.
func Debugw(msg string, fields ...LogField) {
if shallLog(DebugLevel) {
Expand Down Expand Up @@ -229,6 +236,13 @@ func Infov(v any) {
}
}

// Infofn writes function result into access log.
func Infofn(fn func() string) {
if shallLog(InfoLevel) {
writeInfo(fn())
}
}

// Infow writes msg along with fields into the access log.
func Infow(msg string, fields ...LogField) {
if shallLog(InfoLevel) {
Expand Down
42 changes: 41 additions & 1 deletion core/logx/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,26 @@ func TestStructedLogDebugv(t *testing.T) {
Debugv(fmt.Sprint(v...))
})
}

func TestStructedLogDebugfn(t *testing.T) {
w := new(mockWriter)
old := writer.Swap(w)
defer writer.Store(old)
doTestStructedLog(t, levelDebug, w, func(v ...any) {
Debugfn(func() string {
return fmt.Sprint(v...)
})
})
}
func TestDebugfnWithInfoLevel(t *testing.T) {
called := false
SetLevel(InfoLevel)
defer SetLevel(DebugLevel)
Debugfn(func() string {
called = true
return "long time log"
})
assert.False(t, called)
}
func TestStructedLogDebugw(t *testing.T) {
w := new(mockWriter)
old := writer.Swap(w)
Expand Down Expand Up @@ -450,6 +469,27 @@ func TestStructedLogInfoConsoleText(t *testing.T) {
Info(fmt.Sprint(v...))
})
}
func TestStructedInfofn(t *testing.T) {
w := new(mockWriter)
old := writer.Swap(w)
defer writer.Store(old)

doTestStructedLog(t, levelInfo, w, func(v ...any) {
Infofn(func() string {
return fmt.Sprint(v...)
})
})
}
func TestInfofnWithErrorLevel(t *testing.T) {
called := false
SetLevel(ErrorLevel)
defer SetLevel(DebugLevel)
Infofn(func() string {
called = true
return "info log"
})
assert.False(t, called)
}

func TestStructedLogSlow(t *testing.T) {
w := new(mockWriter)
Expand Down
11 changes: 11 additions & 0 deletions core/logx/richlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (l *richLogger) Debugv(v any) {
l.debug(v)
}
}
func (l *richLogger) Debugfn(fn func() string) {
if shallLog(DebugLevel) {
l.debug(fn())
}
}

func (l *richLogger) Debugw(msg string, fields ...LogField) {
if shallLog(DebugLevel) {
Expand Down Expand Up @@ -106,6 +111,12 @@ func (l *richLogger) Infov(v any) {
}
}

func (l *richLogger) Infofn(fn func() string) {
if shallLog(InfoLevel) {
l.info(fn())
}
}

func (l *richLogger) Infow(msg string, fields ...LogField) {
if shallLog(InfoLevel) {
l.info(msg, fields...)
Expand Down

0 comments on commit 64e8c94

Please sign in to comment.