27 lines
541 B
Go
27 lines
541 B
Go
|
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)
|
||
|
}
|
||
|
}
|