mirror of
https://github.com/labstack/echo.git
synced 2024-11-30 08:46:41 +02:00
064a69d1dc
Signed-off-by: Vishal Rana <vr@labstack.com>
33 lines
552 B
Go
33 lines
552 B
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/labstack/echo"
|
|
"github.com/labstack/gommon/color"
|
|
)
|
|
|
|
func Logger(h echo.HandlerFunc) echo.HandlerFunc {
|
|
return echo.HandlerFunc(func(c *echo.Context) {
|
|
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))
|
|
})
|
|
}
|