1
0
mirror of https://github.com/labstack/echo.git synced 2026-05-18 10:01:24 +02:00
Files
echo/middleware/logger.go
T
Vishal Rana 381fbae1ff Default handler now returns error.
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-04-18 16:47:48 -07:00

36 lines
647 B
Go

package middleware
import (
"log"
"time"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
"github.com/mattn/go-colorable"
)
func Logger(h echo.HandlerFunc) (echo.HandlerFunc, error) {
log.SetOutput(colorable.NewColorableStdout())
return func(c *echo.Context) error {
start := time.Now()
h(c)
end := time.Now()
col := color.Green
m := c.Request.Method
p := c.Request.URL.Path
s := c.Response.Status()
switch {
case s >= 500:
col = color.Red
case s >= 400:
col = color.Yellow
case s >= 300:
col = color.Cyan
}
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
return nil
}, nil
}