github.com/orderbynull/buffalo@v0.11.1/middleware/param_logger.go (about) 1 package middleware 2 3 import ( 4 "encoding/json" 5 "mime/multipart" 6 "net/url" 7 8 "github.com/gobuffalo/buffalo" 9 "github.com/pkg/errors" 10 ) 11 12 // ParameterLogger logs form and parameter values to the logger 13 func ParameterLogger(next buffalo.Handler) buffalo.Handler { 14 return func(c buffalo.Context) error { 15 defer func() { 16 req := c.Request() 17 if req.Method != "GET" { 18 if err := postParamLogger(c); err != nil { 19 c.Logger().Error(err) 20 } 21 } 22 b, err := json.Marshal(c.Params()) 23 if err != nil { 24 c.Logger().Error(err) 25 } 26 c.LogField("params", string(b)) 27 }() 28 return next(c) 29 } 30 } 31 32 func postParamLogger(c buffalo.Context) error { 33 req := c.Request() 34 mp := req.MultipartForm 35 if mp != nil { 36 return multipartParamLogger(mp, c) 37 } 38 39 b, err := json.Marshal(req.Form) 40 if err != nil { 41 return errors.WithStack(err) 42 } 43 c.LogField("form", string(b)) 44 return nil 45 } 46 47 func multipartParamLogger(mp *multipart.Form, c buffalo.Context) error { 48 uv := url.Values{} 49 for k, v := range mp.Value { 50 for _, vv := range v { 51 uv.Add(k, vv) 52 } 53 } 54 for k, v := range mp.File { 55 for _, vv := range v { 56 uv.Add(k, vv.Filename) 57 } 58 } 59 b, err := json.Marshal(uv) 60 if err != nil { 61 return errors.WithStack(err) 62 } 63 c.LogField("form", string(b)) 64 return nil 65 }