-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmiddleware.go
50 lines (41 loc) · 1.35 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package negronilogrus
import (
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/negroni"
)
// Middleware is a middleware handler that logs the request as it goes in and the response as it goes out.
type Middleware struct {
// Logger is the log.Logger instance used to log messages with the Logger middleware
Logger *logrus.Logger
}
// NewMiddleware returns a new *Middleware, yay!
func NewMiddleware() *Middleware {
return NewCustomMiddleware(logrus.Info, &logrus.TextFormatter{})
}
// NewCustomMiddleware builds a *Middleware with the given level and formatter
func NewCustomMiddleware(level logrus.Level, formatter logrus.Formatter) *Middleware {
log := logrus.New()
log.Level = level
log.Formatter = formatter
return &Middleware{Logger: log}
}
func (l *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
l.Logger.WithFields(logrus.Fields{
"method": r.Method,
"request": r.RequestURI,
"remote": r.RemoteAddr,
}).Info("started handling request")
next(rw, r)
res := rw.(negroni.ResponseWriter)
l.Logger.WithFields(logrus.Fields{
"status": res.Status(),
"method": r.Method,
"request": r.RequestURI,
"remote": r.RemoteAddr,
"text_status": http.StatusText(res.Status()),
"took": time.Since(start),
}).Info("completed handling request")
}