X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Flogger.go;h=ea7266ba4fe450e4bb406afe7dee7adb693d2b86;hb=e0ab9e8caa9237d3e15193fcb0ca74954afc66be;hpb=4059b21bd00261699ae6c7619adf053a607073e5;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/logger.go b/api/logger.go index 1016863..ea7266b 100644 --- a/api/logger.go +++ b/api/logger.go @@ -11,11 +11,24 @@ import ( var log = utils.StandardL().WithField("module", "api") +func SetContextLogField(c *gin.Context, field string, value interface{}) { + itf, ok := c.Get("logFields") + var fields map[string]interface{} + if !ok { + fields = make(map[string]interface{}) + } else { + fields = itf.(map[string]interface{}) + } + + fields[field] = value + c.Set("logFields", fields) +} + func Logger() gin.HandlerFunc { return func(c *gin.Context) { path := c.Request.URL.Path - start := time.Now() rawQuery := c.Request.URL.RawQuery + start := time.Now() c.Next() @@ -28,8 +41,10 @@ func Logger() gin.HandlerFunc { 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 itf, ok := c.Get("logFields"); ok { + for field, value := range itf.(map[string]interface{}) { + l = l.WithField(field, value) + } } if rawQuery != "" {