Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Debugfn and Infofn to logx/logc #4595 #4598

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
getLogger(ctx).Debugv(v)
}

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

Check warning on line 47 in core/logc/logs.go

View check run for this annotation

Codecov / codecov/patch

core/logc/logs.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

// 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 @@
getLogger(ctx).Infov(v)
}

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

Check warning on line 98 in core/logc/logs.go

View check run for this annotation

Codecov / codecov/patch

core/logc/logs.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

// 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 @@
l.debug(v)
}
}
func (l *richLogger) Debugfn(fn func() string) {
if shallLog(DebugLevel) {
l.debug(fn())
}

Check warning on line 63 in core/logx/richlogger.go

View check run for this annotation

Codecov / codecov/patch

core/logx/richlogger.go#L60-L63

Added lines #L60 - L63 were not covered by tests
}

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

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

Check warning on line 117 in core/logx/richlogger.go

View check run for this annotation

Codecov / codecov/patch

core/logx/richlogger.go#L114-L117

Added lines #L114 - L117 were not covered by tests
}

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