1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

rewrite: Only serialize request if necessary (#6541)

* Prevents serializing the caddy request if log level is not debug.

* Extracts message to const.
This commit is contained in:
Alexander Stecher 2024-08-30 16:46:51 +02:00 committed by GitHub
parent 141c785420
commit ffd28be90a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -131,6 +131,12 @@ func (rewr *Rewrite) Provision(ctx caddy.Context) error {
func (rewr Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { func (rewr Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
const message = "rewrote request"
if rewr.logger.Check(zap.DebugLevel, message) == nil {
rewr.Rewrite(r, repl)
return next.ServeHTTP(w, r)
}
logger := rewr.logger.With( logger := rewr.logger.With(
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}), zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}),
@ -139,7 +145,7 @@ func (rewr Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
changed := rewr.Rewrite(r, repl) changed := rewr.Rewrite(r, repl)
if changed { if changed {
logger.Debug("rewrote request", logger.Debug(message,
zap.String("method", r.Method), zap.String("method", r.Method),
zap.String("uri", r.RequestURI), zap.String("uri", r.RequestURI),
) )