2015-03-01 19:45:13 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/bolt"
|
2015-03-13 00:46:02 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Logger() bolt.HandlerFunc {
|
|
|
|
return func(c *bolt.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
c.Next()
|
|
|
|
end := time.Now()
|
2015-03-13 00:46:02 +02:00
|
|
|
co := color.Green
|
2015-03-01 19:45:13 +02:00
|
|
|
m := c.Request.Method
|
|
|
|
p := c.Request.URL.Path
|
|
|
|
s := c.Response.Status()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case s >= 500:
|
2015-03-13 00:46:02 +02:00
|
|
|
co = color.Red
|
2015-03-01 19:45:13 +02:00
|
|
|
case s >= 400:
|
2015-03-13 00:46:02 +02:00
|
|
|
co = color.Yellow
|
2015-03-01 19:45:13 +02:00
|
|
|
case s >= 300:
|
2015-03-13 00:46:02 +02:00
|
|
|
co = color.Cyan
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 00:46:02 +02:00
|
|
|
log.Printf("%s %s %s %s", m, p, co(s), end.Sub(start))
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|