mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 10:42:57 +00:00
dev day #79
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"fotbal-club/pkg/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RequestLogger logs a concise access log line per request with latency and identifiers.
|
||||
// It is lightweight and safe for production usage.
|
||||
func RequestLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
// Continue
|
||||
c.Next()
|
||||
// After handler
|
||||
status := c.Writer.Status()
|
||||
latency := time.Since(start)
|
||||
rid := c.GetString("request_id")
|
||||
// Try both userID keys used across codebase
|
||||
var uid any
|
||||
if v, ok := c.Get("userID"); ok {
|
||||
uid = v
|
||||
} else if v, ok := c.Get("user_id"); ok {
|
||||
uid = v
|
||||
}
|
||||
if uid != nil {
|
||||
logger.Info("%s %s => %d (%s) rid=%s uid=%v", method, path, status, latency, rid, uid)
|
||||
} else {
|
||||
logger.Info("%s %s => %d (%s) rid=%s", method, path, status, latency, rid)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user