7 "github.com/Sirupsen/logrus"
8 "github.com/gin-gonic/gin"
9 "github.com/jloup/utils"
12 var log = utils.StandardL().WithField("module", "api")
14 func Logger() gin.HandlerFunc {
15 return func(c *gin.Context) {
16 path := c.Request.URL.Path
18 rawQuery := c.Request.URL.RawQuery
22 latency := time.Now().Sub(start).Round(10 * time.Microsecond)
23 code := c.Writer.Status()
26 l = l.WithField("latency", latency)
27 l = l.WithField("client_ip", c.ClientIP())
28 l = l.WithField("method", c.Request.Method)
29 l = l.WithField("status_code", code)
31 if _, exists := c.Get("user"); exists {
32 l = l.WithField("user_id", GetUser(c).Id)
36 path = fmt.Sprintf("%s?%s", path, rawQuery)
39 l = l.WithField("path", path)
41 msgLog := fmt.Sprintf("[%v] %d %s '%s'", latency, code, c.Request.Method, path)
43 var level logrus.Level
45 case code >= 200 && code < 400:
46 level = logrus.DebugLevel
47 case code >= 400 && code < 500:
48 level = logrus.InfoLevel
50 level = logrus.ErrorLevel
53 errors := c.Errors.ByType(gin.ErrorTypePrivate)
55 for _, err := range errors {
56 l.WithField("description", err.Err).Logf(level, "%s: %s", msgLog, err.Err)