log.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "mime/multipart"
  8. "strings"
  9. "time"
  10. "github.com/duke-git/lancet/v2/random"
  11. "github.com/gin-gonic/gin"
  12. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  13. "go.uber.org/zap"
  14. )
  15. const (
  16. MaxBodySize = 10 * 1024 // 10KB
  17. TraceIDKey = "trace_id"
  18. )
  19. var (
  20. // 跳过的路径
  21. skipPaths = map[string]bool{
  22. "/health": true,
  23. "/metrics": true,
  24. "/favicon.ico": true,
  25. "/ping": true,
  26. }
  27. // 需要记录的请求头
  28. importantHeaders = []string{
  29. "authorization",
  30. "x-request-id",
  31. "x-real-ip",
  32. "x-forwarded-for",
  33. "user-agent",
  34. "content-type",
  35. }
  36. )
  37. func RequestLogMiddleware(logger *log.Logger) gin.HandlerFunc {
  38. return func(ctx *gin.Context) {
  39. // 跳过不需要记录的路径
  40. if skipPaths[ctx.Request.URL.Path] {
  41. ctx.Next()
  42. return
  43. }
  44. // 生成简短的追踪ID
  45. traceID := generateTraceID()
  46. ctx.Set(TraceIDKey, traceID)
  47. // 构建日志字段
  48. fields := []zap.Field{
  49. zap.String("trace_id", traceID),
  50. zap.String("method", ctx.Request.Method),
  51. zap.String("path", ctx.Request.URL.Path),
  52. zap.String("client_ip", ctx.ClientIP()),
  53. }
  54. // 记录查询参数
  55. if query := ctx.Request.URL.RawQuery; query != "" {
  56. fields = append(fields, zap.String("query", query))
  57. }
  58. // 记录重要请求头
  59. if headers := getImportantHeaders(ctx); len(headers) > 0 {
  60. fields = append(fields, zap.Any("headers", headers))
  61. }
  62. // 记录请求体(仅限特定方法)
  63. if shouldLogRequestBody(ctx) {
  64. contentType := ctx.GetHeader("Content-Type")
  65. // 特殊处理 multipart/form-data
  66. if strings.Contains(contentType, "multipart/form-data") {
  67. if formData := parseMultipartData(ctx); formData != nil {
  68. fields = append(fields, zap.Any("form_data", formData))
  69. }
  70. } else if strings.Contains(contentType, "application/json") {
  71. // 处理 JSON 请求体
  72. if bodyData := getJSONBody(ctx); bodyData != nil {
  73. fields = append(fields, zap.Any("body", bodyData))
  74. }
  75. } else {
  76. // 处理其他类型的请求体
  77. if bodyLog := getRequestBody(ctx); bodyLog != "" {
  78. fields = append(fields, zap.String("body", bodyLog))
  79. }
  80. }
  81. }
  82. // 设置日志上下文
  83. logger.WithValue(ctx, zap.String("trace_id", traceID))
  84. // 记录请求开始时间
  85. ctx.Set("start_time", time.Now())
  86. logger.Info("API Request", fields...)
  87. ctx.Next()
  88. }
  89. }
  90. func ResponseLogMiddleware(logger *log.Logger) gin.HandlerFunc {
  91. return func(ctx *gin.Context) {
  92. // 跳过不需要记录的路径
  93. if skipPaths[ctx.Request.URL.Path] {
  94. ctx.Next()
  95. return
  96. }
  97. // 包装响应写入器
  98. blw := &bodyLogWriter{
  99. body: bytes.NewBufferString(""),
  100. ResponseWriter: ctx.Writer,
  101. }
  102. ctx.Writer = blw
  103. // 执行处理
  104. ctx.Next()
  105. // 计算耗时
  106. var duration time.Duration
  107. if startTime, exists := ctx.Get("start_time"); exists {
  108. if st, ok := startTime.(time.Time); ok {
  109. duration = time.Since(st)
  110. }
  111. }
  112. // 构建响应日志字段
  113. fields := []zap.Field{
  114. zap.Int("status", ctx.Writer.Status()),
  115. zap.String("duration", duration.String()),
  116. zap.Int64("duration_ms", duration.Milliseconds()),
  117. }
  118. // 记录响应体(限制大小和内容类型)
  119. if shouldLogResponseBody(ctx) {
  120. bodyStr := blw.body.String()
  121. if len(bodyStr) > MaxBodySize {
  122. fields = append(fields, zap.String("body", fmt.Sprintf("[TRUNCATED: %d bytes]", len(bodyStr))))
  123. } else if len(bodyStr) > 0 {
  124. // 尝试解析 JSON 响应
  125. if json.Valid([]byte(bodyStr)) {
  126. var jsonData interface{}
  127. if err := json.Unmarshal([]byte(bodyStr), &jsonData); err == nil {
  128. fields = append(fields, zap.Any("body", jsonData))
  129. } else {
  130. fields = append(fields, zap.String("body", bodyStr))
  131. }
  132. } else {
  133. fields = append(fields, zap.String("body", bodyStr))
  134. }
  135. }
  136. }
  137. // 记录错误信息
  138. if len(ctx.Errors) > 0 {
  139. fields = append(fields, zap.Any("errors", ctx.Errors))
  140. }
  141. // 添加状态分类
  142. statusCode := ctx.Writer.Status()
  143. if statusCode >= 500 {
  144. fields = append(fields, zap.String("level", "error"))
  145. } else if statusCode >= 400 {
  146. fields = append(fields, zap.String("level", "warn"))
  147. } else {
  148. fields = append(fields, zap.String("level", "info"))
  149. }
  150. // 统一使用 Info 级别,避免堆栈信息
  151. logger.WithContext(ctx).Info("API Response", fields...)
  152. }
  153. }
  154. // 获取 JSON 请求体并解析为 map
  155. func getJSONBody(ctx *gin.Context) interface{} {
  156. if ctx.Request.Body == nil {
  157. return nil
  158. }
  159. bodyBytes, err := ctx.GetRawData()
  160. if err != nil {
  161. return nil
  162. }
  163. // 重置请求体
  164. ctx.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
  165. // 检查大小
  166. if len(bodyBytes) == 0 {
  167. return nil
  168. }
  169. if len(bodyBytes) > MaxBodySize {
  170. return fmt.Sprintf("[TRUNCATED: %d bytes]", len(bodyBytes))
  171. }
  172. // 解析 JSON
  173. var data interface{}
  174. if err := json.Unmarshal(bodyBytes, &data); err != nil {
  175. // 如果解析失败,返回原始字符串
  176. return string(bodyBytes)
  177. }
  178. return data
  179. }
  180. // 解析 multipart/form-data
  181. func parseMultipartData(ctx *gin.Context) map[string]interface{} {
  182. // 保存原始请求体
  183. bodyBytes, err := ctx.GetRawData()
  184. if err != nil {
  185. return nil
  186. }
  187. // 重置请求体
  188. ctx.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
  189. // 创建新的 reader
  190. reader := multipart.NewReader(bytes.NewReader(bodyBytes), extractBoundary(ctx.GetHeader("Content-Type")))
  191. if reader == nil {
  192. return nil
  193. }
  194. formData := make(map[string]interface{})
  195. for {
  196. part, err := reader.NextPart()
  197. if err == io.EOF {
  198. break
  199. }
  200. if err != nil {
  201. return nil
  202. }
  203. name := part.FormName()
  204. if name == "" {
  205. continue
  206. }
  207. // 读取内容
  208. value, err := io.ReadAll(part)
  209. if err != nil {
  210. continue
  211. }
  212. valueStr := string(value)
  213. // 尝试解析为 JSON
  214. if json.Valid(value) {
  215. var jsonData interface{}
  216. if err := json.Unmarshal(value, &jsonData); err == nil {
  217. formData[name] = jsonData
  218. } else {
  219. formData[name] = valueStr
  220. }
  221. } else {
  222. formData[name] = valueStr
  223. }
  224. part.Close()
  225. }
  226. return formData
  227. }
  228. // 提取 boundary
  229. func extractBoundary(contentType string) string {
  230. if !strings.Contains(contentType, "boundary=") {
  231. return ""
  232. }
  233. parts := strings.Split(contentType, "boundary=")
  234. if len(parts) < 2 {
  235. return ""
  236. }
  237. boundary := parts[1]
  238. // 移除可能的引号
  239. boundary = strings.Trim(boundary, `"`)
  240. return boundary
  241. }
  242. // bodyLogWriter 包装响应写入器以捕获响应体
  243. type bodyLogWriter struct {
  244. gin.ResponseWriter
  245. body *bytes.Buffer
  246. }
  247. func (w *bodyLogWriter) Write(b []byte) (int, error) {
  248. w.body.Write(b)
  249. return w.ResponseWriter.Write(b)
  250. }
  251. // 辅助函数
  252. func generateTraceID() string {
  253. // 使用时间戳 + 4位随机字符串
  254. return fmt.Sprintf("%d-%s", time.Now().Unix(), random.RandString(4))
  255. }
  256. func getImportantHeaders(ctx *gin.Context) map[string]string {
  257. headers := make(map[string]string)
  258. for _, key := range importantHeaders {
  259. if value := ctx.GetHeader(key); value != "" {
  260. headers[key] = value
  261. }
  262. }
  263. return headers
  264. }
  265. func shouldLogRequestBody(ctx *gin.Context) bool {
  266. method := ctx.Request.Method
  267. return method == "POST" || method == "PUT" || method == "PATCH" || method == "DELETE"
  268. }
  269. func shouldLogResponseBody(ctx *gin.Context) bool {
  270. // 检查内容类型
  271. contentType := ctx.Writer.Header().Get("Content-Type")
  272. return strings.Contains(contentType, "json") ||
  273. strings.Contains(contentType, "xml") ||
  274. strings.Contains(contentType, "text")
  275. }
  276. func getRequestBody(ctx *gin.Context) string {
  277. if ctx.Request.Body == nil {
  278. return ""
  279. }
  280. bodyBytes, err := ctx.GetRawData()
  281. if err != nil {
  282. return ""
  283. }
  284. // 重置请求体
  285. ctx.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
  286. // 检查大小
  287. if len(bodyBytes) == 0 {
  288. return ""
  289. }
  290. if len(bodyBytes) > MaxBodySize {
  291. return fmt.Sprintf("[TRUNCATED: %d bytes]", len(bodyBytes))
  292. }
  293. return string(bodyBytes)
  294. }