You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.1 KiB

  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "strings"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // LogLayout 日志layout
  12. type LogLayout struct {
  13. Time time.Time
  14. Metadata map[string]interface{} // 存储自定义原数据
  15. Path string // 访问路径
  16. Query string // 携带query
  17. Body string // 携带body数据
  18. IP string // ip地址
  19. UserAgent string // 代理
  20. Error string // 错误
  21. Cost time.Duration // 花费时间
  22. Source string // 来源
  23. }
  24. type Logger struct {
  25. // Filter 用户自定义过滤
  26. Filter func(c *gin.Context) bool
  27. // FilterKeyword 关键字过滤(key)
  28. FilterKeyword func(layout *LogLayout) bool
  29. // AuthProcess 鉴权处理
  30. AuthProcess func(c *gin.Context, layout *LogLayout)
  31. // 日志处理
  32. Print func(LogLayout)
  33. // Source 服务唯一标识
  34. Source string
  35. }
  36. func (l Logger) SetLoggerMiddleware() gin.HandlerFunc {
  37. return func(c *gin.Context) {
  38. start := time.Now()
  39. path := c.Request.URL.Path
  40. query := c.Request.URL.RawQuery
  41. var body []byte
  42. if l.Filter != nil && !l.Filter(c) {
  43. body, _ = c.GetRawData()
  44. // 将原body塞回去
  45. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
  46. }
  47. c.Next()
  48. cost := time.Since(start)
  49. layout := LogLayout{
  50. Time: time.Now(),
  51. Path: path,
  52. Query: query,
  53. IP: c.ClientIP(),
  54. UserAgent: c.Request.UserAgent(),
  55. Error: strings.TrimRight(c.Errors.ByType(gin.ErrorTypePrivate).String(), "\n"),
  56. Cost: cost,
  57. Source: l.Source,
  58. }
  59. if l.Filter != nil && !l.Filter(c) {
  60. layout.Body = string(body)
  61. }
  62. // 处理鉴权需要的信息
  63. l.AuthProcess(c, &layout)
  64. if l.FilterKeyword != nil {
  65. // 自行判断key/value 脱敏等
  66. l.FilterKeyword(&layout)
  67. }
  68. // 自行处理日志
  69. l.Print(layout)
  70. }
  71. }
  72. func DefaultLogger() gin.HandlerFunc {
  73. return Logger{
  74. Print: func(layout LogLayout) {
  75. // 标准输出,k8s做收集
  76. v, _ := json.Marshal(layout)
  77. fmt.Println(string(v))
  78. },
  79. Source: "GVA",
  80. }.SetLoggerMiddleware()
  81. }