Skip to content

Commit a140bef

Browse files
committed
Override ReverseProxy's error logger and replace format statements
with an error and a warning logs Signed-off-by: Doğukan Teber <[email protected]>
1 parent 0a903a8 commit a140bef

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

gateway/proxy.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ package gateway
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"net"
78
"net/http"
89
"net/http/httputil"
910
"net/url"
11+
"os"
1012
"time"
13+
14+
"github.com/sirupsen/logrus"
1115
)
1216

1317
const (
@@ -50,6 +54,13 @@ type Proxy struct {
5054
reverseProxy *httputil.ReverseProxy
5155
}
5256

57+
type logrusErrorWriter struct{}
58+
59+
func (w logrusErrorWriter) Write(p []byte) (n int, err error) {
60+
logrus.Errorf("%s", string(p))
61+
return len(p), nil
62+
}
63+
5364
func NewProxy(targetURL string, upstream Upstream, component string) (*Proxy, error) {
5465
url, err := url.Parse(targetURL)
5566
if err != nil {
@@ -63,6 +74,7 @@ func NewProxy(targetURL string, upstream Upstream, component string) (*Proxy, er
6374
reverseProxy.Transport = customTransport(component, upstream)
6475
originalDirector := reverseProxy.Director
6576
reverseProxy.Director = customDirector(url, originalDirector)
77+
reverseProxy.ErrorLog = log.New(logrusErrorWriter{}, "", 0)
6678

6779
if upstream.HTTPClientTimeout == 0 {
6880
upstream.HTTPClientTimeout = defaultTimeoutValues[component].HTTPClientTimeout
@@ -102,8 +114,8 @@ func customTransport(component string, upstream Upstream) http.RoundTripper {
102114

103115
url, err := url.Parse(upstream.URL)
104116
if err != nil {
105-
// TODO: log the error with logrus
106-
fmt.Println(err)
117+
logrus.Errorf("unexpected error when parsing the upstream url: %s", err)
118+
os.Exit(1)
107119
}
108120

109121
resolver := DefaultDNSResolver{}

server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/prometheus/client_golang/prometheus"
1313
"github.com/prometheus/client_golang/prometheus/promauto"
1414
"github.com/prometheus/client_golang/prometheus/promhttp"
15+
"github.com/sirupsen/logrus"
1516
)
1617

1718
const (
@@ -172,8 +173,7 @@ func (s *Server) RegisterTo(pattern string, handler http.Handler, where string)
172173
case UNAUTH:
173174
s.unAuthServer.http.Handle(pattern, handler)
174175
default:
175-
// TODO: replace this with a logger or something else
176-
fmt.Println("unknown")
176+
logrus.Warnf("unexpected parameter: %s, valid options: %s, %s, %s not registered", where, AUTH, UNAUTH, pattern)
177177
}
178178
}
179179

0 commit comments

Comments
 (0)