Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcserver: Forward HTTP server err msgs to logger. #2378

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io"
"io/ioutil"
stdlog "log"
"math"
"math/big"
"net"
Expand Down Expand Up @@ -5858,6 +5859,17 @@ func jsonAuthFail(w http.ResponseWriter) {
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
}

// logForwarder provides logic to forward log messages writing to an io.Writer
// to the rpcserver logger.
type logForwarder struct{}

// Write implements the io.Writer interface and forwards the message to the
// active rpcserver logger.
func (logForwarder) Write(p []byte) (int, error) {
log.Error(strings.TrimRight(string(p), "\r\n"))
return len(p), nil
}

// route sets up the endpoints of the rpc server.
func (s *Server) route(ctx context.Context) *http.Server {
rpcServeMux := http.NewServeMux()
Expand All @@ -5867,6 +5879,10 @@ func (s *Server) route(ctx context.Context) *http.Server {
// Timeout connections which don't complete the initial
// handshake within the allowed timeframe.
ReadTimeout: time.Second * rpcAuthTimeoutSeconds,

// Reroute http server error logging through the rpcserver
// logger.
ErrorLog: stdlog.New(logForwarder{}, "", 0),
}
rpcServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close")
Expand Down