]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/commitdiff
Augment log messages.
authorjloup <jloup@jloup.work>
Wed, 16 May 2018 17:44:11 +0000 (19:44 +0200)
committerjloup <jloup@jloup.work>
Wed, 16 May 2018 17:44:11 +0000 (19:44 +0200)
api/logger.go
cmd/app/main.go

index 7057a3046b0dcfe2a8d6063fa80f4640630a1ec3..a24e08a34b8b726b751eafaef168d436af72b5ec 100644 (file)
@@ -1,6 +1,11 @@
 package api
 
 import (
+       "fmt"
+       "strings"
+       "time"
+
+       "github.com/Sirupsen/logrus"
        "github.com/gin-gonic/gin"
        "github.com/jloup/utils"
 )
@@ -10,18 +15,47 @@ var log = utils.StandardL().WithField("module", "api")
 func Logger() gin.HandlerFunc {
        return func(c *gin.Context) {
                path := c.Request.URL.Path
+               start := time.Now()
                rawQuery := c.Request.URL.RawQuery
 
                c.Next()
 
-               for _, err := range c.Errors {
-                       l := log.WithField("path", path)
+               latency := time.Now().Sub(start).Round(10 * time.Microsecond)
+               code := c.Writer.Status()
+
+               l := log
+               l = l.WithField("latency", latency)
+               l = l.WithField("client_ip", c.ClientIP())
+               l = l.WithField("method", c.Request.Method)
+               l = l.WithField("status_code", code)
+
+               if _, exists := c.Get("user"); exists {
+                       l = l.WithField("user_id", GetUser(c).Id)
+               }
+
+               if rawQuery != "" {
+                       path = fmt.Sprintf("%s?%s", path, rawQuery)
+               }
+
+               l = l.WithField("path", path)
 
-                       if rawQuery != "" {
-                               l = l.WithField("query", rawQuery)
-                       }
+               msgLog := fmt.Sprintf("[%v] %d %s '%s'", latency, code, c.Request.Method, path)
 
-                       l.Errorf("%s", err.Err)
+               var level logrus.Level
+               switch {
+               case code >= 200 && code < 400:
+                       level = logrus.InfoLevel
+               case code >= 400 && code < 500:
+                       level = logrus.WarnLevel
+               default:
+                       level = logrus.ErrorLevel
                }
+
+               comment := c.Errors.ByType(gin.ErrorTypePrivate).String()
+               if comment != "" {
+                       msgLog = fmt.Sprintf("%s: %s", msgLog, strings.TrimSpace(comment))
+               }
+
+               l.Logf(level, msgLog)
        }
 }
index d663e3d62d8898dfac5d5013c4ba90213df2901d..63feef7952975c1db540054e8bf9f55e8cf7639a 100644 (file)
@@ -61,17 +61,17 @@ func init() {
                panic(err)
        }
 
-       journalhook.Enable()
-       api.SetConfig(C.Api.Config)
-       api.SetMailConfig(C.Mail)
-
-       db.Init(C.Db, C.Redis)
-
        if C.Mode == "prod" {
                gin.DisableConsoleColor()
                gin.SetMode(gin.ReleaseMode)
+               journalhook.Enable()
        }
 
+       api.SetConfig(C.Api.Config)
+       api.SetMailConfig(C.Mail)
+
+       db.Init(C.Db, C.Redis)
+
        log.Infof("CONFIG:")
        log.Infof("LISTEN: %s", strings.Join([]string{C.Address, C.Port}, ":"))
        log.Infof("PUBLIC_DIR: %s", C.App.PublicDir)