-
Notifications
You must be signed in to change notification settings - Fork 2
/
ginerus.go
55 lines (45 loc) · 1.01 KB
/
ginerus.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
55
package ginerus
import (
"fmt"
"time"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)
func Ginerus() gin.HandlerFunc {
return GinerusWithLogger(logrus.StandardLogger())
}
func GinerusWithLogger(logger *logrus.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
c.Next()
end := time.Now()
latency := end.Sub(start)
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
comment := c.Errors.String()
userAgent := c.Request.UserAgent()
timeFormatted := end.Format("2006-01-02 15:04:05")
msg := fmt.Sprintf(
"%s %s \"%s %s\" %d %s %s",
clientIP,
timeFormatted,
method,
path,
statusCode,
latency,
userAgent,
)
logger.WithFields(logrus.Fields{
"time": timeFormatted,
"method": method,
"path": path,
"latency": latency,
"ip": clientIP,
"comment": comment,
"status": statusCode,
"user-agent": userAgent,
}).Info(msg)
}
}