feat: add request logger middleware

This commit is contained in:
2026-05-20 17:05:05 +02:00
committed by Mikkel Elvers
parent 6b84335515
commit 2cfab673f6
3 changed files with 77 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
package server
import (
"log"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
func RequestLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
route := c.FullPath()
if route == "" {
route = path
}
log.Printf(
"http_request method=%s route=%s path=%s query=%s status=%d duration_ms=%.2f bytes=%d client_ip=%s errors=%s",
c.Request.Method,
strconv.Quote(route),
strconv.Quote(path),
strconv.Quote(query),
c.Writer.Status(),
float64(time.Since(start).Microseconds())/1000,
c.Writer.Size(),
strconv.Quote(c.ClientIP()),
strconv.Quote(c.Errors.ByType(gin.ErrorTypePrivate).String()),
)
}
}