summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Theunissen <mark.theunissen@gmail.com>2014-08-21 10:16:34 -0700
committerMark Theunissen <mark.theunissen@gmail.com>2014-08-21 10:16:34 -0700
commit8a1f04f9d7caae4c4d2fa2f126c3d6c31046d623 (patch)
treeb75d0455512e15937442c44bbcd387b4e7c04d3d
parenta050eb441c28e4c93cfbd829f04351c201e88c25 (diff)
downloadgo-8a1f04f9d7caae4c4d2fa2f126c3d6c31046d623.tar.gz
net/http/httputil: Pass a Logger to ReverseProxy, allowing the user to control logging.
Fixes issue 8553. LGTM=bradfitz R=golang-codereviews, dave, bradfitz CC=golang-codereviews https://codereview.appspot.com/132750043 Committer: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/pkg/net/http/httputil/reverseproxy.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/pkg/net/http/httputil/reverseproxy.go b/src/pkg/net/http/httputil/reverseproxy.go
index 48ada5f5f..ab4637018 100644
--- a/src/pkg/net/http/httputil/reverseproxy.go
+++ b/src/pkg/net/http/httputil/reverseproxy.go
@@ -40,6 +40,12 @@ type ReverseProxy struct {
// response body.
// If zero, no periodic flushing is done.
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 {
@@ -138,7 +144,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
res, err := transport.RoundTrip(outreq)
if err != nil {
- log.Printf("http: proxy error: %v", err)
+ p.logf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
@@ -171,6 +177,14 @@ func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
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 {
io.Writer
http.Flusher