mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
Moved middleware in a package
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
35
middleware/logger.go
Normal file
35
middleware/logger.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
"github.com/labstack/gommon/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Logger(h echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c *echo.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
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user