Open
Description
Is your feature request related to a problem? Please describe.
Today at times we'll call spew
on a struct so we can get a pretty printable version.
With the way the log statement/function works, the arg will be evaluated even if the log level doesn't apply. As an example, if the log level is info
, but a trace
log uses spew, the struct will still get spewed even if we're not logging it. This can eat p some CPU over time
Describe the solution you'd like
We should start to use newLogClosure
for all instances where we might spew something:
// logClosure is used to provide a closure over expensive logging operations so
// don't have to be performed when the logging level doesn't warrant it.
type logClosure func() string
// String invokes the underlying function and returns the result.
func (c logClosure) String() string {
return c()
}
// newLogClosure returns a new closure over a function that returns a string
// which itself provides a Stringer interface so that it can be used with the
// logging system.
func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
We can make a higher order function to make the setup+call a bit easier to use.