1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package fslog
- import (
- "bytes"
- ginzap "github.com/gin-contrib/zap"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- "io"
- "time"
- )
- func GinLogger() gin.HandlerFunc {
- lazyInitialize()
- return ginzap.GinzapWithConfig(logger, &ginzap.Config{
- UTC: true,
- TimeFormat: time.RFC3339,
- Context: func(c *gin.Context) []zapcore.Field {
- var body []byte
- var buf bytes.Buffer
- fields := make([]zapcore.Field, 0)
- body, err := io.ReadAll(c.Request.Body)
- if err != nil {
- return make([]zapcore.Field, 0)
- }
- if body != nil && len(body) != 0 {
- fields = append(fields, zap.String("body", string(body)))
- c.Request.Body = io.NopCloser(&buf)
- }
- return fields
- },
- })
- }
- func GinRecovery() gin.HandlerFunc {
- lazyInitialize()
- return ginzap.RecoveryWithZap(logger, true)
- }
|