Skip to content

Commit

Permalink
F errorlog log wrapper (#89)
Browse files Browse the repository at this point in the history
* catch error log of http and reverse proxy handler, so logging ends up in the log instead of stderr

* update change log

* better description

* add proper comments
  • Loading branch information
rdoorn authored Aug 15, 2019
1 parent 561271c commit 8319c3d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Bug:
Change:
* Removed manual websocket implementation for httputil's reverse proxy. it now supports websockets in the main branch
* DNS forwarder library replaced with one that correctly resolves cnames recursively
* Error logging of the http/reverseproxy server is now logged in the log file instead of stderr

## 0.14.4
Feature:
Expand Down
30 changes: 30 additions & 0 deletions pkg/logging/log-logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package logging

import (
stdlog "log"
)

// StandardLogWriterInterface is an interface to wrap log.Logger
type StandardLogWriterInterface interface {
Write(p []byte) (n int, err error)
}

// StandardLogWrite implements StandardLogWriterInterface
type StandardLogWrite struct {
//buf bytes.Buffer
name string
}

// Write is the write handler for StandardLog
func (s StandardLogWrite) Write(p []byte) (n int, err error) {
str := string(p)
For(s.name).Warn(str)
return len(p), nil
}

// StandardLog returns a log.Logger wrapper
func StandardLog(name string) *stdlog.Logger {
w := StandardLogWrite{name: name}
std := stdlog.New(w, "", stdlog.Lshortfile)
return std
}
9 changes: 9 additions & 0 deletions pkg/logging/log-logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package logging

import "testing"

func TestStandardLogger(t *testing.T) {
Configure("stdout", "debug")
l := StandardLog("test/log")
l.Print("log entry\n")
}
1 change: 1 addition & 0 deletions pkg/proxy/proxy_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func (l *Listener) NewHTTPProxy() *httputil.ReverseProxy {
Transport: transport,
ModifyResponse: modifyresponse,
FlushInterval: 250 * time.Millisecond, // good for streams adn server-sent events
ErrorLog: logging.StandardLog("httpproxy/reverseproxy"),

// ErrorLog: logrus not supported as logging until v2 => https://github.com/golang/go/issues/21082
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/proxy/proxy_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (l *Listener) Start() {
WriteTimeout: time.Duration(l.WriteTimeout) * time.Second,
Addr: fmt.Sprintf("%s:%d", l.IP, l.Port),
Handler: proxy,
ErrorLog: logging.StandardLog("listener/http"),
}

listener, err = net.Listen("tcp", httpsrv.Addr)
Expand Down Expand Up @@ -139,6 +140,7 @@ func (l *Listener) Start() {
Addr: fmt.Sprintf("%s:%d", l.IP, l.Port),
Handler: proxy,
TLSConfig: l.TLSConfig,
ErrorLog: logging.StandardLog("listener/https"),
//TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}

Expand Down

0 comments on commit 8319c3d

Please sign in to comment.