2015-04-23 10:44:50 +02:00
|
|
|
package middleware
|
2015-04-21 08:24:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/gommon/color"
|
|
|
|
)
|
|
|
|
|
2015-05-15 21:29:14 +02:00
|
|
|
func Logger() echo.MiddlewareFunc {
|
|
|
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c *echo.Context) *echo.HTTPError {
|
|
|
|
start := time.Now()
|
|
|
|
if he := h(c); he != nil {
|
|
|
|
c.Error(he)
|
|
|
|
}
|
|
|
|
end := time.Now()
|
|
|
|
method := c.Request.Method
|
|
|
|
path := c.Request.URL.Path
|
|
|
|
if path == "" {
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
size := c.Response.Size()
|
2015-04-21 08:24:57 +02:00
|
|
|
|
2015-05-15 21:29:14 +02:00
|
|
|
n := c.Response.Status()
|
|
|
|
code := color.Green(n)
|
|
|
|
switch {
|
|
|
|
case n >= 500:
|
|
|
|
code = color.Red(n)
|
|
|
|
case n >= 400:
|
|
|
|
code = color.Yellow(n)
|
|
|
|
case n >= 300:
|
|
|
|
code = color.Cyan(n)
|
|
|
|
}
|
2015-04-21 08:24:57 +02:00
|
|
|
|
2015-05-15 21:29:14 +02:00
|
|
|
log.Printf("%s %s %s %s %d", method, path, code, end.Sub(start), size)
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-21 08:24:57 +02:00
|
|
|
}
|
|
|
|
}
|