mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
@@ -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...)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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...)
|
||||
|
||||
Reference in New Issue
Block a user