Skip to content

Commit

Permalink
Add lazyDebugf method to process the arguments lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Apr 22, 2024
1 parent 34c7675 commit 3e8fd39
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/grpclog/prefixLogger.go
Expand Up @@ -79,6 +79,34 @@ func (pl *PrefixLogger) Debugf(format string, args ...any) {

}

// DebugfLazy does info logging at verbose level 2, only
// evaluating arguments if logging is enabled.
func (pl *PrefixLogger) DebugfLazy(format string, args ...func() any) {
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

0 comments on commit 3e8fd39

Please sign in to comment.