package middleware

import (
	"Go_server/config"
	"Go_server/helper"
	"os"
	"path"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

// 日志中间件
func LoggerToFile() gin.HandlerFunc {
	logFilePath := config.Config.Log
	if err := helper.IsDirExists(logFilePath); err != nil {
		panic(err)
	}
	fileName := path.Join(logFilePath, "syslog.log")
	src, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
	if err != nil {
		panic(err)
	}

	logger := logrus.New()
	logger.Out = src
	logger.SetLevel(logrus.DebugLevel)
	logger.SetFormatter(&logrus.TextFormatter{})

	return func(c *gin.Context) {
		startTime := time.Now()
		c.Next()
		endTime := time.Now()

		latencyTime := endTime.Sub(startTime)
		reqMethod := c.Request.Method
		reqUri := c.Request.URL
		statusCode := c.Writer.Status()
		clientIP := c.ClientIP()
		logger.SetFormatter(&logrus.TextFormatter{
			TimestampFormat: "2006-01-02 15:04:05",
		})
		logger.Infof("| %3d | %13v | %15s | %4s | %s |",
			statusCode,
			latencyTime,
			clientIP,
			reqMethod,
			reqUri,
		)
	}
}
