goweb/middleware/logger.go

40 lines
1014 B
Go
Raw Permalink Normal View History

package middleware
import (
"github.com/gin-gonic/gin"
2021-08-22 00:13:00 +08:00
"goweb/global"
"time"
)
// ZapLogger 接收gin框架的路由日志
func ZapLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
cost := time.Since(start)
//global.Log.Info(path,
// zap.Int("status", c.Writer.Status()),
// zap.String("method", c.Request.Method),
// zap.String("path", path),
// zap.String("query", query),
// zap.String("ip", c.ClientIP()),
// zap.String("user-agent", c.Request.UserAgent()),
// zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
// zap.Duration("cost", cost),
//)
logTmpl := `
=================================================================================
Path: %v
Param: %v
Method: %v
IP: %v
Cost: %v
=================================================================================
`
global.Log.Debugf(logTmpl, path, query, c.Request.Method, c.ClientIP(), cost)
}
}