mirror of
https://github.com/labstack/echo.git
synced 2026-05-16 09:48:24 +02:00
+10
-10
@@ -6,30 +6,30 @@ import (
|
||||
|
||||
"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())
|
||||
func Logger(h echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
start := time.Now()
|
||||
h(c)
|
||||
if err := h(c); err != nil {
|
||||
return err
|
||||
}
|
||||
end := time.Now()
|
||||
col := color.Green
|
||||
m := c.Request.Method
|
||||
p := c.Request.URL.Path
|
||||
s := c.Response.Status()
|
||||
n := c.Response.Status()
|
||||
|
||||
switch {
|
||||
case s >= 500:
|
||||
case n >= 500:
|
||||
col = color.Red
|
||||
case s >= 400:
|
||||
case n >= 400:
|
||||
col = color.Yellow
|
||||
case s >= 300:
|
||||
case n >= 300:
|
||||
col = color.Cyan
|
||||
}
|
||||
|
||||
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
|
||||
log.Printf("%s %s %s %s", m, p, col(n), end.Sub(start))
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user