2025-04-02 14:29:57 +08:00

44 lines
1020 B
Go

package middleware
import (
"log"
"time"
"github.com/gofiber/fiber/v2"
)
// DebugMiddleware 用于记录更详细的请求信息以便排查问题
func DebugMiddleware() fiber.Handler {
return func(c *fiber.Ctx) error {
// 记录开始时间
start := time.Now()
// 输出请求详情
log.Printf("[DEBUG] 收到请求: %s %s", c.Method(), c.Path())
log.Printf("[DEBUG] 查询参数: %s", c.Request().URI().QueryArgs().String())
log.Printf("[DEBUG] 请求头: %v", c.GetReqHeaders())
// 处理请求
err := c.Next()
// 请求结束,计算耗时
duration := time.Since(start)
status := c.Response().StatusCode()
if err != nil {
log.Printf("[DEBUG] 请求错误: %s %s - %d - %v - %s",
c.Method(), c.Path(), status, err, duration)
} else {
log.Printf("[DEBUG] 请求完成: %s %s - %d - %s",
c.Method(), c.Path(), status, duration)
}
if status == 500 {
log.Printf("[ERROR] 内部服务器错误: %s %s - %v",
c.Method(), c.Path(), err)
}
return err
}
}