operation.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package middleware
  2. import (
  3. "bytes"
  4. "fmt"
  5. "gin-vue-admin/global"
  6. "github.com/gin-gonic/gin"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  10. )
  11. var body []byte
  12. func RecordRequestBody() gin.HandlerFunc {
  13. return func(c *gin.Context) {
  14. if c.Request.Method != http.MethodGet {
  15. var err error
  16. body, err = ioutil.ReadAll(c.Request.Body)
  17. if err != nil {
  18. global.GVA_LOG.Error(err)
  19. }
  20. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
  21. } else {
  22. body = nil
  23. }
  24. }
  25. }
  26. func OperationRecord() gin.HandlerFunc {
  27. return gin.LoggerWithConfig(gin.LoggerConfig{
  28. Formatter: func(param gin.LogFormatterParams) string {
  29. return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" \"%s\" %s\"\n",
  30. param.ClientIP,
  31. param.TimeStamp.Format(time.RFC1123),
  32. param.Method,
  33. param.Path,
  34. param.Request.Proto,
  35. param.StatusCode,
  36. param.Latency,
  37. param.Request.UserAgent(),
  38. string(body),
  39. param.ErrorMessage,
  40. )
  41. },
  42. // 暂时没考虑好
  43. Output: nil,
  44. SkipPaths: global.GVA_CONFIG.Operation.SkipPaths,
  45. })
  46. }