1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
echo/middleware/logger.go
Vishal Rana bf85c56b08 Encapsulated fields and exposed public functions.
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-22 04:40:01 -07:00

42 lines
773 B
Go

package middleware
import (
"log"
"time"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
)
func Logger() echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
start := time.Now()
if err := h(c); err != nil {
c.Error(err)
}
end := time.Now()
method := c.Request().Method
path := c.Request().URL.Path
if path == "" {
path = "/"
}
size := c.Response().Size()
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)
}
log.Printf("%s %s %s %s %d", method, path, code, end.Sub(start), size)
return nil
}
}
}