2015-04-20 01:00:23 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-04-20 01:04:31 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
func Logger(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) error {
|
2015-03-01 19:45:13 +02:00
|
|
|
start := time.Now()
|
2015-04-19 06:53:38 +02:00
|
|
|
if err := h(c); err != nil {
|
2015-04-20 01:00:23 +02:00
|
|
|
c.Error(err)
|
2015-04-19 06:53:38 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
end := time.Now()
|
|
|
|
m := c.Request.Method
|
|
|
|
p := c.Request.URL.Path
|
2015-04-19 06:53:38 +02:00
|
|
|
n := c.Response.Status()
|
2015-04-20 01:00:23 +02:00
|
|
|
col := color.Green
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
switch {
|
2015-04-19 06:53:38 +02:00
|
|
|
case n >= 500:
|
2015-03-13 05:50:33 +02:00
|
|
|
col = color.Red
|
2015-04-19 06:53:38 +02:00
|
|
|
case n >= 400:
|
2015-03-13 05:50:33 +02:00
|
|
|
col = color.Yellow
|
2015-04-19 06:53:38 +02:00
|
|
|
case n >= 300:
|
2015-03-13 05:50:33 +02:00
|
|
|
col = color.Cyan
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 06:53:38 +02:00
|
|
|
log.Printf("%s %s %s %s", m, p, col(n), end.Sub(start))
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-04-19 06:53:38 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|