mirror of
https://github.com/caddyserver/caddy.git
synced 2025-06-01 09:02:50 +08:00
v2: Logging! (#2831)
* logging: Initial implementation * logging: More encoder formats, better defaults * logging: Fix repetition bug with FilterEncoder; add more presets * logging: DiscardWriter; delete or no-op logs that discard their output * logging: Add http.handlers.log module; enhance Replacer methods The Replacer interface has new methods to customize how to handle empty or unrecognized placeholders. Closes #2815. * logging: Overhaul HTTP logging, fix bugs, improve filtering, etc. * logging: General cleanup, begin transitioning to using new loggers * Fixes after merge conflict
This commit is contained in:
@ -18,6 +18,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
weakrand "math/rand"
|
||||
"mime"
|
||||
"net/http"
|
||||
@ -191,6 +192,24 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, _ cadd
|
||||
}
|
||||
}
|
||||
|
||||
// if this handler exists in an error context (i.e. is
|
||||
// part of a handler chain that is supposed to handle
|
||||
// a previous error), we have to serve the content
|
||||
// manually in order to write the correct status code
|
||||
if reqErr, ok := r.Context().Value(caddyhttp.ErrorCtxKey).(error); ok {
|
||||
statusCode := http.StatusInternalServerError
|
||||
if handlerErr, ok := reqErr.(caddyhttp.HandlerError); ok {
|
||||
if handlerErr.StatusCode > 0 {
|
||||
statusCode = handlerErr.StatusCode
|
||||
}
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
if r.Method != "HEAD" {
|
||||
io.Copy(w, file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// let the standard library do what it does best; note, however,
|
||||
// that errors generated by ServeContent are written immediately
|
||||
// to the response, so we cannot handle them (but errors there
|
||||
|
Reference in New Issue
Block a user