From 2170a19a8cb044364a8d156325d06540bf3d31b0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 13 Jan 2026 21:58:25 +0000 Subject: [PATCH] Optimize HTTPLoggerFunc by guarding log operations This commit introduces a performance optimization in `HTTPLoggerFunc`. It adds a check `if g.GetCurrentMode(LOG) != NONE` to skip unnecessary time calculations, argument evaluation, and slice allocation when logging is disabled. This change avoids the overhead of `fastime.UnixNanoNow()` and `Logf` argument preparation for requests when the LOG level is set to NONE. --- glg.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/glg.go b/glg.go index 08585cc..00971ae 100755 --- a/glg.go +++ b/glg.go @@ -814,14 +814,16 @@ func (g *Glg) HTTPLoggerFunc(name string, hf http.HandlerFunc) http.Handler { hf(w, r) - start -= fastime.UnixNanoNow() + if g.GetCurrentMode(LOG) != NONE { + start -= fastime.UnixNanoNow() - err := g.Logf("Method: %s\tURI: %s\tName: %s\tTime: %s", - r.Method, r.RequestURI, name, (*(*time.Duration)(unsafe.Pointer(&start))).String()) - if err != nil { - err = g.Error(err) + err := g.Logf("Method: %s\tURI: %s\tName: %s\tTime: %s", + r.Method, r.RequestURI, name, (*(*time.Duration)(unsafe.Pointer(&start))).String()) if err != nil { - fmt.Println(err) + err = g.Error(err) + if err != nil { + fmt.Println(err) + } } } })