37 lines
808 B
Go
37 lines
808 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
|
||
|
|
"github.com/yourorg/go-dw-platform/framework/logger"
|
||
|
|
"github.com/yourorg/go-dw-platform/framework/metrics"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Logging(log *logger.Logger) gin.HandlerFunc {
|
||
|
|
return func(c *gin.Context) {
|
||
|
|
requestID := c.GetHeader("X-Request-ID")
|
||
|
|
if requestID == "" {
|
||
|
|
requestID = uuid.NewString()
|
||
|
|
}
|
||
|
|
c.Set("request_id", requestID)
|
||
|
|
|
||
|
|
start := time.Now()
|
||
|
|
c.Next()
|
||
|
|
duration := time.Since(start)
|
||
|
|
|
||
|
|
log.Info("request handled",
|
||
|
|
"request_id", requestID,
|
||
|
|
"method", c.Request.Method,
|
||
|
|
"path", c.Request.URL.Path,
|
||
|
|
"status", c.Writer.Status(),
|
||
|
|
"duration_ms", duration.Milliseconds(),
|
||
|
|
)
|
||
|
|
|
||
|
|
metrics.RecordHTTPRequest(c.Request.Method, c.FullPath(), strconv.Itoa(c.Writer.Status()), duration)
|
||
|
|
}
|
||
|
|
}
|