package middleware

import (
	"DataShare/global"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func LogHook() gin.HandlerFunc {

	return func(c *gin.Context) {
		c.Next()
		//请求方式
		method:=c.Request.Method
		//请求路由
		reqUrl := c.Request.RequestURI
		//状态码
		statusCode := c.Writer.Status()
		//请求ip
		clientIP := c.ClientIP()
		//请求参数:
		params,exist := c.Get("params")
		if exist == false {
			global.SystemLogger.WithFields(logrus.Fields{
				"status_code":  statusCode,
				"client_ip":    clientIP,
				"req_method":   method,
				"req_uri":      reqUrl,
			}).Info()
		}else{
			global.SystemLogger.WithFields(logrus.Fields{
				"status_code":  statusCode,
				"client_ip":    clientIP,
				"req_method":   method,
				"req_uri":      reqUrl,
				"params":       params,

			}).Info()
		}
	}
}
