Skip to content

Commit

Permalink
caddyhttp: Optimize logs using zap's WithLazy() (#6590)
Browse files Browse the repository at this point in the history
* uses zap's .WithLazy with a cloned request

* fixes the cloning

* adds comment explaining why cloning is faster
  • Loading branch information
AlliBalliBaba authored Sep 26, 2024
1 parent 2faeac0 commit 22c98ea
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,18 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// encode the request for logging purposes before
// clone the request for logging purposes before
// it enters any handler chain; this is necessary
// to capture the original request in case it gets
// modified during handling
// cloning the request and using .WithLazy is considerably faster
// than using .With, which will JSON encode the request immediately
shouldLogCredentials := s.Logs != nil && s.Logs.ShouldLogCredentials
loggableReq := zap.Object("request", LoggableHTTPRequest{
Request: r,
Request: r.Clone(r.Context()),
ShouldLogCredentials: shouldLogCredentials,
})
errLog := s.errorLogger.With(loggableReq)
errLog := s.errorLogger.WithLazy(loggableReq)

var duration time.Duration

Expand Down

0 comments on commit 22c98ea

Please sign in to comment.