logger.go 651 字节
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1 2 3 4
package middleware

import (
	"github.com/gin-gonic/gin"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
5
	"main/init/qmlog"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
	"time"
)

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 开始时间
		start := time.Now()
		// 处理请求
		c.Next()
		// 结束时间
		end := time.Now()
		//执行时间
		latency := end.Sub(start)

		path := c.Request.URL.Path
		clientIP := c.ClientIP()
		method := c.Request.Method
		statusCode := c.Writer.Status()
		buf := make([]byte, 1024)
		n, _ := c.Request.Body.Read(buf)
		requestParams := buf[0:n]
Mr.奇淼('s avatar
Mr.奇淼( 已提交
27
		qmlog.QMLog.Infof("| %3d | %13v | %15s | %s  %s |%s|",
Mr.奇淼('s avatar
Mr.奇淼( 已提交
28 29 30 31 32 33 34
			statusCode,
			latency,
			clientIP,
			method, path, requestParams,
		)
	}
}