-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttplog.go
54 lines (45 loc) · 1.32 KB
/
httplog.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
51
52
53
54
package httplog
import (
"log"
"net/http"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
)
// Logger is a middleware handler that logs the request as it goes in and the response as it goes out.
type Logger struct {
// Logger inherits from log.Logger used to log messages with the Logger middleware
*log.Logger
jsonOut bool
}
// NewLogger returns a new Logger instance
func NewLogger(jsonOut bool) *Logger {
return &Logger{log.New(os.Stdout, "[http log] ", 0), jsonOut}
}
// ServerHTTP negroni middleware interface
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
err := r.ParseForm()
if err != nil {
l.Println("ParseForm error", err)
}
param := r.Form.Encode()
next(rw, r)
res := rw.(negroni.ResponseWriter)
if l.jsonOut {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.WithFields(map[string]interface{}{
"Addr": r.RemoteAddr,
"Now": time.Now(),
"Method": r.Method,
"URLPath": r.URL.Path,
"ResStatus": res.Status(),
"StartAt": time.Since(start),
"Agent": r.UserAgent(),
"Param": param,
}).Info("[http log]")
} else {
l.Printf("%s - [%v] \"%s %s\" %d %v %s \"%s\"", r.RemoteAddr, time.Now(), r.Method, r.URL.Path, res.Status(), time.Since(start), r.UserAgent(), param)
}
}