logger.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  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 = io.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. if l.AuthProcess != nil {
  63. // 处理鉴权需要的信息
  64. l.AuthProcess(c, &layout)
  65. }
  66. if l.FilterKeyword != nil {
  67. // 自行判断key/value 脱敏等
  68. l.FilterKeyword(&layout)
  69. }
  70. // 自行处理日志
  71. l.Print(layout)
  72. }
  73. }
  74. func DefaultLogger() gin.HandlerFunc {
  75. return Logger{
  76. Print: func(layout LogLayout) {
  77. // 标准输出,k8s做收集
  78. v, _ := json.Marshal(layout)
  79. fmt.Println(string(v))
  80. },
  81. Source: "GVA",
  82. }.SetLoggerMiddleware()
  83. }