yjp 1 gadu atpakaļ
vecāks
revīzija
86c2b6c725
1 mainītis faili ar 7 papildinājumiem un 4 dzēšanām
  1. 7 4
      gin_logger.go

+ 7 - 4
gin_logger.go

@@ -18,16 +18,19 @@ func GinLogger() gin.HandlerFunc {
 		Context: func(c *gin.Context) []zapcore.Field {
 			var body []byte
 			var buf bytes.Buffer
+			fields := make([]zapcore.Field, 0)
 
-			tee := io.TeeReader(c.Request.Body, &buf)
-			body, err := io.ReadAll(tee)
+			body, err := io.ReadAll(c.Request.Body)
 			if err != nil {
 				return make([]zapcore.Field, 0)
 			}
 
-			c.Request.Body = io.NopCloser(&buf)
+			if body != nil && len(body) != 0 {
+				fields = append(fields, zap.String("body", string(body)))
+				c.Request.Body = io.NopCloser(&buf)
+			}
 
-			return []zapcore.Field{zap.String("body", string(body))}
+			return fields
 		},
 	})
 }