switched to gin-gonic
This commit is contained in:
@@ -1,17 +1,33 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/logger"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"GoMembership/pkg/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// LoggerMiddleware logs each incoming HTTP request
|
||||
func LoggerMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
logger.Info.Printf("%s %s %s", r.Method, r.RequestURI, time.Since(start))
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
// 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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user