aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjloup <jloup@jloup.work>2018-05-17 00:09:08 +0200
committerjloup <jloup@jloup.work>2018-05-17 00:09:08 +0200
commite0ab9e8caa9237d3e15193fcb0ca74954afc66be (patch)
tree43588e5cd109ca7d3f2f7e5f6785fc2113768b97
parent4059b21bd00261699ae6c7619adf053a607073e5 (diff)
downloadFront-e0ab9e8caa9237d3e15193fcb0ca74954afc66be.tar.gz
Front-e0ab9e8caa9237d3e15193fcb0ca74954afc66be.tar.zst
Front-e0ab9e8caa9237d3e15193fcb0ca74954afc66be.zip
Context log fields.v0.0.19
-rw-r--r--api/auth_jwt.go1
-rw-r--r--api/logger.go21
2 files changed, 19 insertions, 3 deletions
diff --git a/api/auth_jwt.go b/api/auth_jwt.go
index 88ccda8..27cc3b0 100644
--- a/api/auth_jwt.go
+++ b/api/auth_jwt.go
@@ -92,6 +92,7 @@ func JwtAuth(c *gin.Context) *Error {
92 92
93 c.Set("user", *user) 93 c.Set("user", *user)
94 c.Set("claims", claims) 94 c.Set("claims", claims)
95 SetContextLogField(c, "user_id", user.Id)
95 96
96 return nil 97 return nil
97} 98}
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 (
11 11
12var log = utils.StandardL().WithField("module", "api") 12var log = utils.StandardL().WithField("module", "api")
13 13
14func SetContextLogField(c *gin.Context, field string, value interface{}) {
15 itf, ok := c.Get("logFields")
16 var fields map[string]interface{}
17 if !ok {
18 fields = make(map[string]interface{})
19 } else {
20 fields = itf.(map[string]interface{})
21 }
22
23 fields[field] = value
24 c.Set("logFields", fields)
25}
26
14func Logger() gin.HandlerFunc { 27func Logger() gin.HandlerFunc {
15 return func(c *gin.Context) { 28 return func(c *gin.Context) {
16 path := c.Request.URL.Path 29 path := c.Request.URL.Path
17 start := time.Now()
18 rawQuery := c.Request.URL.RawQuery 30 rawQuery := c.Request.URL.RawQuery
31 start := time.Now()
19 32
20 c.Next() 33 c.Next()
21 34
@@ -28,8 +41,10 @@ func Logger() gin.HandlerFunc {
28 l = l.WithField("method", c.Request.Method) 41 l = l.WithField("method", c.Request.Method)
29 l = l.WithField("status_code", code) 42 l = l.WithField("status_code", code)
30 43
31 if _, exists := c.Get("user"); exists { 44 if itf, ok := c.Get("logFields"); ok {
32 l = l.WithField("user_id", GetUser(c).Id) 45 for field, value := range itf.(map[string]interface{}) {
46 l = l.WithField(field, value)
47 }
33 } 48 }
34 49
35 if rawQuery != "" { 50 if rawQuery != "" {