Skip to content

Commit

Permalink
Revert "Log request headers, response headers and some body as warnin…
Browse files Browse the repository at this point in the history
…g on 500s" (#56)

* Revert "Use new gRPC APIs (#55)"

This reverts commit df441dc.

* Revert "Log request headers, response headers and some body as warning on 500s (#41)"

This reverts commit c8c93e3.
  • Loading branch information
jml authored Sep 18, 2017
1 parent df441dc commit 81b6ecf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 102 deletions.
38 changes: 13 additions & 25 deletions middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,40 @@ package middleware

import (
"bufio"
"bytes"
"fmt"
"net"
"net/http"
"net/http/httputil"
"time"

log "github.com/Sirupsen/logrus"

"github.com/weaveworks/common/user"
)

// Log middleware logs http requests
type Log struct {
LogRequestHeaders bool // LogRequestHeaders true -> dump http headers at debug log level
}

// logWithRequest information from the request and context as fields.
func logWithRequest(r *http.Request) *log.Entry {
return log.WithFields(user.LogFields(r.Context()))
}

// Wrap implements Middleware
func (l Log) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
uri := r.RequestURI // capture the URI before running next, as it may get rewritten
// Log headers before running 'next' in case other interceptors change the data.
headers, err := httputil.DumpRequest(r, false)
if err != nil {
headers = nil
logWithRequest(r).Errorf("Could not dump request headers: %v", err)
}
var buf bytes.Buffer
wrapped := newBadResponseLoggingWriter(w, &buf)
next.ServeHTTP(wrapped, r)
statusCode := wrapped.statusCode
if 100 <= statusCode && statusCode < 400 {
logWithRequest(r).Debugf("%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
if l.LogRequestHeaders && headers != nil {
logWithRequest(r).Debugf("Is websocket request: %v\n%s", IsWSHandshakeRequest(r), string(headers))
if l.LogRequestHeaders {
// Log headers before running 'next' in case other interceptors change the data.
headers, err := httputil.DumpRequest(r, false)
if err != nil {
log.Warnf("Could not dump request headers: %v", err)
return
}
log.Debugf("Is websocket request: %v\n%s", IsWSHandshakeRequest(r), string(headers))
}
i := &interceptor{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(i, r)
if 100 <= i.statusCode && i.statusCode < 400 {
log.Debugf("%s %s (%d) %s", r.Method, uri, i.statusCode, time.Since(begin))
} else {
logWithRequest(r).Warnf("%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
logWithRequest(r).Warnf("Is websocket request: %v\n%s", IsWSHandshakeRequest(r), string(headers))
logWithRequest(r).Warnf("Response: %s", buf.Bytes())
log.Warnf("%s %s (%d) %s", r.Method, uri, i.statusCode, time.Since(begin))
}
})
}
Expand Down
77 changes: 0 additions & 77 deletions middleware/response.go

This file was deleted.

0 comments on commit 81b6ecf

Please sign in to comment.