gin-logrus/logger.go

47 lines
1.0 KiB
Go
Raw Normal View History

2016-09-26 17:22:16 +03:00
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)
}
}
}
}