@marcel91200 / caddy Mirror · read-only / commits / 22c98ea1
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 08:15 UTC

caddyhttp: Optimize logs using zap's WithLazy() (#6590)

* uses zap's .WithLazy with a cloned request * fixes the cloning * adds comment explaining why cloning is faster

Alexander Stecher committed Sep 26, 2024 at 20:23 UTC 22c98ea165bdfbca33fbc77ce3b2bd22d3ee4626
1 file changed +5 -3
modules/caddyhttp/server.go
+5 -3
@@ -313,16 +313,18 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
313 }
314 }
315
316 - // encode the request for logging purposes before
316 + // clone the request for logging purposes before
317 // it enters any handler chain; this is necessary
318 // to capture the original request in case it gets
319 // modified during handling
320 + // cloning the request and using .WithLazy is considerably faster
321 + // than using .With, which will JSON encode the request immediately
322 shouldLogCredentials := s.Logs != nil && s.Logs.ShouldLogCredentials
323 loggableReq := zap.Object("request", LoggableHTTPRequest{
322 - Request: r,
324 + Request: r.Clone(r.Context()),
325 ShouldLogCredentials: shouldLogCredentials,
326 })
325 - errLog := s.errorLogger.With(loggableReq)
327 + errLog := s.errorLogger.WithLazy(loggableReq)
328
329 var duration time.Duration
330