gin_logger.go 730 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package fslog
  2. import (
  3. "bytes"
  4. ginzap "github.com/gin-contrib/zap"
  5. "github.com/gin-gonic/gin"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. "io"
  9. "time"
  10. )
  11. func GinLogger() gin.HandlerFunc {
  12. lazyInitialize()
  13. return ginzap.GinzapWithConfig(logger, &ginzap.Config{
  14. UTC: true,
  15. TimeFormat: time.RFC3339,
  16. Context: func(c *gin.Context) []zapcore.Field {
  17. var body []byte
  18. var buf bytes.Buffer
  19. body, err := io.ReadAll(c.Request.Body)
  20. if err != nil {
  21. return make([]zapcore.Field, 0)
  22. }
  23. c.Request.Body = io.NopCloser(&buf)
  24. return []zapcore.Field{zap.String("body", string(body))}
  25. },
  26. })
  27. }
  28. func GinRecovery() gin.HandlerFunc {
  29. lazyInitialize()
  30. return ginzap.RecoveryWithZap(logger, true)
  31. }