2020-01-21 15:12:26 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger instances a Logger middleware for Gin.
|
|
|
|
func Logger() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
// Start timer
|
|
|
|
start := time.Now()
|
|
|
|
path := c.Request.URL.Path
|
|
|
|
raw := c.Request.URL.RawQuery
|
|
|
|
|
|
|
|
// Process request
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
// Stop timer
|
|
|
|
end := time.Now()
|
|
|
|
latency := end.Sub(start)
|
|
|
|
|
2020-01-22 13:43:07 +01:00
|
|
|
// clientIP := c.ClientIP()
|
2020-01-21 15:12:26 +01:00
|
|
|
method := c.Request.Method
|
|
|
|
statusCode := c.Writer.Status()
|
|
|
|
|
|
|
|
if raw != "" {
|
|
|
|
path = path + "?" + raw
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode >= 400 {
|
2020-01-22 13:43:07 +01:00
|
|
|
log.Errorf("%s %s (%3d) [%v]",
|
2020-01-21 15:12:26 +01:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
statusCode,
|
|
|
|
latency,
|
|
|
|
)
|
|
|
|
} else {
|
2020-01-22 13:43:07 +01:00
|
|
|
log.Debugf("%s %s (%3d) [%v]",
|
2020-01-21 15:12:26 +01:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
statusCode,
|
|
|
|
latency,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|