Skip to content

Commit 22c98ea

Browse files
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
1 parent 2faeac0 commit 22c98ea

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

modules/caddyhttp/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,18 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
313313
}
314314
}
315315

316-
// encode the request for logging purposes before
316+
// clone the request for logging purposes before
317317
// it enters any handler chain; this is necessary
318318
// to capture the original request in case it gets
319319
// modified during handling
320+
// cloning the request and using .WithLazy is considerably faster
321+
// than using .With, which will JSON encode the request immediately
320322
shouldLogCredentials := s.Logs != nil && s.Logs.ShouldLogCredentials
321323
loggableReq := zap.Object("request", LoggableHTTPRequest{
322-
Request: r,
324+
Request: r.Clone(r.Context()),
323325
ShouldLogCredentials: shouldLogCredentials,
324326
})
325-
errLog := s.errorLogger.With(loggableReq)
327+
errLog := s.errorLogger.WithLazy(loggableReq)
326328

327329
var duration time.Duration
328330

0 commit comments

Comments
 (0)