diff --git a/core/logc/logs.go b/core/logc/logs.go index b8343fa26ce8..e3af5f8c6eb6 100644 --- a/core/logc/logs.go +++ b/core/logc/logs.go @@ -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...) @@ -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...) diff --git a/core/logx/logger.go b/core/logx/logger.go index 796e62e99ac6..ca236d1108b7 100644 --- a/core/logx/logger.go +++ b/core/logx/logger.go @@ -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. @@ -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. diff --git a/core/logx/logs.go b/core/logx/logs.go index 2f84cd00fdd0..631c413f8010 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -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) { @@ -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) { diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index c79c68da9579..35a609a47830 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -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) @@ -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) diff --git a/core/logx/richlogger.go b/core/logx/richlogger.go index 482852e43493..8af6bf999020 100644 --- a/core/logx/richlogger.go +++ b/core/logx/richlogger.go @@ -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) { @@ -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...)