online-order/api/middlewares/log/requestLoggerMiddleware.go

27 lines
541 B
Go
Raw Normal View History

2023-10-31 16:31:06 +00:00
package middlewares_log
import (
"fmt"
"github.com/gin-gonic/gin"
"os"
"time"
)
func RequestLogger(logFile *os.File) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
end := time.Now()
status := c.Writer.Status()
method := c.Request.Method
path := c.Request.URL.Path
host := c.Request.Host
latency := end.Sub(start)
logLine := fmt.Sprintf("%s | %3d | %s | %s | %s | %13v \n", end.Format("2006/01/02 - 15:04:05"), status, latency, method, host, path)
logFile.WriteString(logLine)
}
}