1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-23 21:29:26 +02:00
echo/middleware/logger.go
Vishal Rana f80fff4efb issue #50
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-05 21:55:49 -07:00

36 lines
604 B
Go

package middleware
import (
"log"
"time"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
)
func Logger(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()
m := c.Request.Method
p := c.Request.URL.Path
n := c.Response.Status()
col := color.Green
switch {
case n >= 500:
col = color.Red
case n >= 400:
col = color.Yellow
case n >= 300:
col = color.Cyan
}
log.Printf("%s %s %s %s", m, p, col(n), end.Sub(start))
return nil
}
}