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 lazyDebugf method to process the arguments lazily #7158

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/grpclog/prefixLogger.go
Expand Up @@ -79,6 +79,34 @@ func (pl *PrefixLogger) Debugf(format string, args ...any) {

}

// LazyDebugf does info logging at verbose level 2, only
// evaluating arguments if logging is enabled.
func (pl *PrefixLogger) LazyDebugf(format string, args ...func() any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this worth it?

This makes the call site:

logger.LazyDebugf("blah %s %s %v", func() { blah1() }, func() { blah2() }, func() { blah3() })

vs.

if logger.V(2) {
	logger.Infof("blah %s %s %v", blah1(), blah2(), blah3())
}

Yes it's "3 lines instead of 1 line" but:

  1. it's not obvious that Debugf is checking V(2), and
  2. the usage with Lazy is really hard to read and very long because of all the extraneous closures.

I think I'd rather just delete Debugf instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many occurrences of Debugf do we have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

17 i believe

if !Logger.V(2) {
return
}
if pl != nil {
format = pl.prefix + format

// Build the argument list by evaluating the closures
var evaluatedArgs []any
for _, argFunc := range args {
evaluatedArgs = append(evaluatedArgs, argFunc())
}

pl.logger.InfoDepth(1, fmt.Sprintf(format, evaluatedArgs...))
return
}

// Build the argument list by evaluating the closures
var evaluatedArgs []any
for _, argFunc := range args {
evaluatedArgs = append(evaluatedArgs, argFunc())
}

InfoDepth(1, fmt.Sprintf(format, evaluatedArgs...))
}

// V reports whether verbosity level l is at least the requested verbose level.
func (pl *PrefixLogger) V(l int) bool {
// TODO(6044): Refactor interfaces LoggerV2 and DepthLogger, and maybe
Expand Down