package middleware

import (
	"github.com/gin-gonic/gin"
	"testData/global"
	"testData/model"
	"time"
)

func Log() gin.HandlerFunc {
	return func(c *gin.Context) {
		startTime := time.Now()
		c.Next()
		statusCode := c.Writer.Status()
		endTime := time.Now()
		u, _ := c.Get("claims")
		user := &model.User{}
		if u != nil {
			user = u.(*model.User)
		} else {
			user.Username = "未登录"
		}
		latencyTime := endTime.Sub(startTime)
		reqMethod := c.Request.Method
		reqUri := c.Request.RequestURI
		clientIp := c.ClientIP()
		global.Log.Infof("| %15s | %3d | %15s | %6s | %s | %s",
			clientIp, statusCode, latencyTime, reqMethod, user.Username, reqUri)
	}
}
