gin-logrus/logger.go

47 lines
1.0 KiB
Go

package ginlogrus
import (
"fmt"
"math"
"time"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)
// Logger is the logrus logger handler
func Logger(log *logrus.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
// other handler can change c.Path so:
path := c.Request.URL.Path
start := time.Now()
c.Next()
stop := time.Since(start)
statusCode := c.Writer.Status()
latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000.0))
clientIP := c.ClientIP()
entry := logrus.NewEntry(log).WithFields(logrus.Fields{
"statusCode": statusCode,
"latency": latency, // time to process
"clientIP": clientIP,
"method": c.Request.Method,
"path": path,
})
if len(c.Errors) > 0 {
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
} else {
msg := fmt.Sprintf("%s - %s %s %d (%dms)", clientIP, c.Request.Method, path, statusCode, latency)
if statusCode > 499 {
entry.Error(msg)
} else if statusCode > 399 {
entry.Warn(msg)
} else {
entry.Info(msg)
}
}
}
}