mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
3eeea660fa
Signed-off-by: Vishal Rana <vr@labstack.com>
35 lines
541 B
Go
35 lines
541 B
Go
package echo
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"labstack.com/gommon/color"
|
|
)
|
|
|
|
func Logger(h HandlerFunc) HandlerFunc {
|
|
return func(c *Context) error {
|
|
start := time.Now()
|
|
if err := h(c); err != nil {
|
|
c.Error(err)
|
|
}
|
|
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
|
|
}
|
|
}
|