Add request logging to panic recovery #63

Merged
electricdusk merged 1 commits from issue-61 into master 2020-07-06 17:13:03 +02:00

View File

@ -30,11 +30,15 @@ func (rl *rushlink) RootURL() *url.URL {
func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logRequestInfo := func() {
log.Printf("in request: %v - %v %q %v", r.RemoteAddr, r.Method, r.RequestURI, r.Proto)
}
defer func() {
Outdated
Review

I don't see a reason why the source port needs to be chopped off. Besides, the net/http server guarantees (by means of documentation) that http.Request.RemoteAddr is filled using IP:port, so this check should not be necessary.

I don't see a reason why the source port needs to be chopped off. Besides, the `net/http` server guarantees (by means of documentation) that `http.Request.RemoteAddr` is filled using `IP:port`, so this check should not be necessary.
defer func() {
if err := recover(); err != nil {
w.WriteHeader(500)
log.Printf("error: panic while recovering from another panic: %v\n", err)
Outdated
Review

log.Printf already registers the timestamp. What's the use of a second timestamp here?

`log.Printf` already registers the timestamp. What's the use of a second timestamp here?
logRequestInfo()
Outdated
Review

Instead of '%s', you may use %q, which automatically quotes the supplied string.

Instead of `'%s'`, you may use `%q`, which automatically quotes the supplied string.
debug.PrintStack()
fmt.Fprintf(w, "internal server error: %v\n", err)
}
@ -43,6 +47,7 @@ func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
if err := recover(); err != nil {
w.WriteHeader(500)
log.Printf("error: %v\n", err)
logRequestInfo()
debug.PrintStack()
rl.renderInternalServerError(w, r, err)
}