8 "github.com/Sirupsen/logrus"
9 "github.com/gin-gonic/gin"
10 "github.com/jloup/utils"
13 var log = utils.StandardL().WithField("module", "api")
15 func Logger() gin.HandlerFunc {
16 return func(c *gin.Context) {
17 path := c.Request.URL.Path
19 rawQuery := c.Request.URL.RawQuery
23 latency := time.Now().Sub(start).Round(10 * time.Microsecond)
24 code := c.Writer.Status()
27 l = l.WithField("latency", latency)
28 l = l.WithField("client_ip", c.ClientIP())
29 l = l.WithField("method", c.Request.Method)
30 l = l.WithField("status_code", code)
32 if _, exists := c.Get("user"); exists {
33 l = l.WithField("user_id", GetUser(c).Id)
37 path = fmt.Sprintf("%s?%s", path, rawQuery)
40 l = l.WithField("path", path)
42 msgLog := fmt.Sprintf("[%v] %d %s '%s'", latency, code, c.Request.Method, path)
44 var level logrus.Level
46 case code >= 200 && code < 400:
47 level = logrus.InfoLevel
48 case code >= 400 && code < 500:
49 level = logrus.WarnLevel
51 level = logrus.ErrorLevel
54 comment := c.Errors.ByType(gin.ErrorTypePrivate).String()
56 msgLog = fmt.Sprintf("%s: %s", msgLog, strings.TrimSpace(comment))