40 lines
1020 B
Go
40 lines
1020 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"go_api_tmpl/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)
|
|
}
|
|
}
|