Commit 1a1d4323 authored by Mark Theunissen's avatar Mark Theunissen Committed by Brad Fitzpatrick

net/http/httputil: Pass a Logger to ReverseProxy, allowing the user to control logging.

Fixes #8553.

LGTM=bradfitz
R=golang-codereviews, dave, bradfitz
CC=golang-codereviews
https://golang.org/cl/132750043
parent b2cc08b1
...@@ -40,6 +40,12 @@ type ReverseProxy struct { ...@@ -40,6 +40,12 @@ type ReverseProxy struct {
// response body. // response body.
// If zero, no periodic flushing is done. // If zero, no periodic flushing is done.
FlushInterval time.Duration FlushInterval time.Duration
// ErrorLog specifies an optional logger for errors
// that occur when attempting to proxy the request.
// If nil, logging goes to os.Stderr via the log package's
// standard logger.
ErrorLog *log.Logger
} }
func singleJoiningSlash(a, b string) string { func singleJoiningSlash(a, b string) string {
...@@ -138,7 +144,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { ...@@ -138,7 +144,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
res, err := transport.RoundTrip(outreq) res, err := transport.RoundTrip(outreq)
if err != nil { if err != nil {
log.Printf("http: proxy error: %v", err) p.logf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
return return
} }
...@@ -171,6 +177,14 @@ func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { ...@@ -171,6 +177,14 @@ func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
io.Copy(dst, src) io.Copy(dst, src)
} }
func (p *ReverseProxy) logf(format string, args ...interface{}) {
if p.ErrorLog != nil {
p.ErrorLog.Printf(format, args...)
} else {
log.Printf(format, args...)
}
}
type writeFlusher interface { type writeFlusher interface {
io.Writer io.Writer
http.Flusher http.Flusher
......
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