34 lines
581 B
Go
34 lines
581 B
Go
package middlewares
|
|
|
|
import (
|
|
"time"
|
|
|
|
"GoMembership/pkg/logger"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// LoggerMiddleware logs the incoming requests.
|
|
func LoggerMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
startTime := time.Now()
|
|
|
|
// Process the request
|
|
c.Next()
|
|
|
|
// Calculate the latency
|
|
latency := time.Since(startTime)
|
|
|
|
// Get the status code
|
|
statusCode := c.Writer.Status()
|
|
|
|
// Log the details
|
|
logger.Info.Printf("| %3d | %13v | %15s | %-7s %#v\n",
|
|
statusCode,
|
|
latency,
|
|
c.ClientIP(),
|
|
c.Request.Method,
|
|
c.Request.URL.Path,
|
|
)
|
|
}
|
|
}
|