]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/httputil: Pass a Logger to ReverseProxy, allowing the user to control logging.
authorMark Theunissen <mark.theunissen@gmail.com>
Thu, 21 Aug 2014 17:16:34 +0000 (10:16 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 21 Aug 2014 17:16:34 +0000 (10:16 -0700)
Fixes #8553.

LGTM=bradfitz
R=golang-codereviews, dave, bradfitz
CC=golang-codereviews
https://golang.org/cl/132750043

src/pkg/net/http/httputil/reverseproxy.go

index 48ada5f5fdbf1774a5895db8f2ecdecf33f974e9..ab463701803bd13cdfbff1c16856424fd25b6fd5 100644 (file)
@@ -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