Commit c811d416 authored by Matthew Holt's avatar Matthew Holt

log: Customizable default error function

parent 92391bfd
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
caddylog "github.com/mholt/caddy/middleware/log" caddylog "github.com/mholt/caddy/middleware/log"
"github.com/mholt/caddy/server"
) )
// Log sets up the logging middleware. // Log sets up the logging middleware.
...@@ -39,7 +40,7 @@ func Log(c *Controller) (middleware.Middleware, error) { ...@@ -39,7 +40,7 @@ func Log(c *Controller) (middleware.Middleware, error) {
}) })
return func(next middleware.Handler) middleware.Handler { return func(next middleware.Handler) middleware.Handler {
return caddylog.Logger{Next: next, Rules: rules} return caddylog.Logger{Next: next, Rules: rules, ErrorFunc: server.DefaultErrorFunc}
}, nil }, nil
} }
......
...@@ -11,8 +11,9 @@ import ( ...@@ -11,8 +11,9 @@ import (
// Logger is a basic request logging middleware. // Logger is a basic request logging middleware.
type Logger struct { type Logger struct {
Next middleware.Handler Next middleware.Handler
Rules []Rule Rules []Rule
ErrorFunc func(http.ResponseWriter, *http.Request, int) // failover error handler
} }
func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...@@ -21,8 +22,15 @@ func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -21,8 +22,15 @@ func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
responseRecorder := middleware.NewResponseRecorder(w) responseRecorder := middleware.NewResponseRecorder(w)
status, err := l.Next.ServeHTTP(responseRecorder, r) status, err := l.Next.ServeHTTP(responseRecorder, r)
if status >= 400 { if status >= 400 {
responseRecorder.WriteHeader(status) // There was an error up the chain, but no response has been written yet.
fmt.Fprintf(responseRecorder, "%d %s", status, http.StatusText(status)) // The error must be handled here so the log entry will record the response size.
if l.ErrorFunc != nil {
l.ErrorFunc(responseRecorder, r, status)
} else {
// Default failover error handler
responseRecorder.WriteHeader(status)
fmt.Fprintf(responseRecorder, "%d %s", status, http.StatusText(status))
}
status = 0 status = 0
} }
rep := middleware.NewReplacer(r, responseRecorder) rep := middleware.NewReplacer(r, responseRecorder)
......
...@@ -221,11 +221,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -221,11 +221,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Fallback error response in case error handling wasn't chained in // Fallback error response in case error handling wasn't chained in
if status >= 400 { if status >= 400 {
w.WriteHeader(status) DefaultErrorFunc(w, r, status)
fmt.Fprintf(w, "%d %s", status, http.StatusText(status))
} }
} else { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "No such host at %s", s.address) fmt.Fprintf(w, "No such host at %s", s.address)
} }
} }
func DefaultErrorFunc(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
fmt.Fprintf(w, "%d %s", status, http.StatusText(status))
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment