git » gofer » commit 2cbf4e7

trace: Only call log.Log if it's going to be useful

author Alberto Bertogli
2022-09-25 10:12:31 UTC
committer Alberto Bertogli
2022-10-07 10:59:43 UTC
parent b13b9eab9aabab003872fca2597bda157582f084

trace: Only call log.Log if it's going to be useful

Calling log.Log is not necessary most of the time, since it is only
useful when running with debug verbosity. However, the string formatting
is a small but not insignificant cost when serving small files.

This patch makes the log.Log call conditional to the log level, to
avoid formatting the string unnecessarily.

trace/trace.go +4 -2

diff --git a/trace/trace.go b/trace/trace.go
index 34b5418..7a3480e 100644
--- a/trace/trace.go
+++ b/trace/trace.go
@@ -44,8 +44,10 @@ func (t *Trace) SetMaxEvents(n int) {
 func (t *Trace) Printf(format string, a ...interface{}) {
 	t.t.Printf(format, a...)
 
-	log.Log(log.Debug, 1, "%#p %s %s: %s",
-		t, t.family, t.title, fmt.Sprintf(format, a...))
+	if log.Default.Level <= log.Debug {
+		log.Log(log.Debug, 1, "%#p %s %s: %s", t, t.family, t.title,
+			fmt.Sprintf(format, a...))
+	}
 }
 
 // Errorf adds this message to the trace's log, with an error level.