commit
This commit is contained in:
@@ -27,9 +27,12 @@ func RequestLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
startTime := time.Now()
|
||||
|
||||
// 读取请求体
|
||||
// 读取请求体(跳过文件上传)
|
||||
var requestBody []byte
|
||||
if c.Request.Body != nil {
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
|
||||
// 如果不是文件上传,才读取请求体
|
||||
if c.Request.Body != nil && !strings.HasPrefix(contentType, "multipart/form-data") {
|
||||
requestBody, _ = io.ReadAll(c.Request.Body)
|
||||
// 恢复请求体供后续处理使用
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||
@@ -61,14 +64,14 @@ func printRequest(c *gin.Context, body []byte) {
|
||||
fmt.Println("\n" + strings.Repeat("=", 100))
|
||||
fmt.Printf("📥 [REQUEST] %s\n", time.Now().Format("2006-01-02 15:04:05"))
|
||||
fmt.Println(strings.Repeat("=", 100))
|
||||
|
||||
|
||||
// 请求基本信息
|
||||
fmt.Printf("Method: %s\n", c.Request.Method)
|
||||
fmt.Printf("Path: %s\n", c.Request.URL.Path)
|
||||
fmt.Printf("Full URL: %s\n", c.Request.URL.String())
|
||||
fmt.Printf("Client IP: %s\n", c.ClientIP())
|
||||
fmt.Printf("User-Agent: %s\n", c.Request.UserAgent())
|
||||
|
||||
|
||||
// 请求头
|
||||
if len(c.Request.Header) > 0 {
|
||||
fmt.Println("\n--- Headers ---")
|
||||
@@ -100,6 +103,9 @@ func printRequest(c *gin.Context, body []byte) {
|
||||
} else {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
} else if strings.HasPrefix(c.GetHeader("Content-Type"), "multipart/form-data") {
|
||||
fmt.Println("\n--- Request Body ---")
|
||||
fmt.Println("[File upload: multipart/form-data]")
|
||||
}
|
||||
|
||||
fmt.Println(strings.Repeat("-", 100))
|
||||
@@ -110,11 +116,11 @@ func printResponse(c *gin.Context, body []byte, duration time.Duration) {
|
||||
fmt.Println("\n" + strings.Repeat("=", 100))
|
||||
fmt.Printf("📤 [RESPONSE] %s | Duration: %v\n", time.Now().Format("2006-01-02 15:04:05"), duration)
|
||||
fmt.Println(strings.Repeat("=", 100))
|
||||
|
||||
|
||||
// 响应基本信息
|
||||
fmt.Printf("Status Code: %d %s\n", c.Writer.Status(), getStatusText(c.Writer.Status()))
|
||||
fmt.Printf("Size: %d bytes\n", c.Writer.Size())
|
||||
|
||||
|
||||
// 响应头
|
||||
if len(c.Writer.Header()) > 0 {
|
||||
fmt.Println("\n--- Response Headers ---")
|
||||
@@ -126,12 +132,21 @@ func printResponse(c *gin.Context, body []byte, duration time.Duration) {
|
||||
// 响应体
|
||||
if len(body) > 0 {
|
||||
fmt.Println("\n--- Response Body ---")
|
||||
// 尝试格式化 JSON
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, body, "", " "); err == nil {
|
||||
fmt.Println(prettyJSON.String())
|
||||
|
||||
// 检查Content-Type,跳过二进制数据
|
||||
contentType := c.Writer.Header().Get("Content-Type")
|
||||
if strings.Contains(contentType, "image/") ||
|
||||
strings.Contains(contentType, "application/octet-stream") ||
|
||||
len(body) > 10240 { // 超过10KB的响应不打印
|
||||
fmt.Printf("[Binary data: %d bytes, Content-Type: %s]\n", len(body), contentType)
|
||||
} else {
|
||||
fmt.Println(string(body))
|
||||
// 尝试格式化 JSON
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, body, "", " "); err == nil {
|
||||
fmt.Println(prettyJSON.String())
|
||||
} else {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user