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()
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 != "" {