tracker

用于在客户提交类的请求添加跟踪日志,可输出query、body以及params等信息,并可设置正则匹配将关键数据加*处理。

Example

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/vicanso/elton"
  7. "github.com/vicanso/elton/middleware"
  8. )
  9. func main() {
  10. e := elton.New()
  11. loginTracker := middleware.NewTracker(middleware.TrackerConfig{
  12. OnTrack: func(info *middleware.TrackerInfo, _ *elton.Context) {
  13. buf, _ := json.Marshal(info)
  14. fmt.Println(string(buf))
  15. },
  16. })
  17. e.Use(func(c *elton.Context) error {
  18. c.RequestBody = []byte(`{
  19. "account": "tree.xie",
  20. "password": "123456"
  21. }`)
  22. return c.Next()
  23. })
  24. e.POST("/user/login", loginTracker, func(c *elton.Context) (err error) {
  25. c.SetHeader(elton.HeaderContentType, elton.MIMEApplicationJSON)
  26. c.BodyBuffer = bytes.NewBuffer(c.RequestBody)
  27. return
  28. })
  29. err := e.ListenAndServe(":3000")
  30. if err != nil {
  31. panic(err)
  32. }
  33. }