mirror of
https://github.com/labstack/echo.git
synced 2025-12-01 22:51:17 +02:00
32
middleware/logger.go
Normal file
32
middleware/logger.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/bolt"
|
||||
"labstack.com/common/utils"
|
||||
)
|
||||
|
||||
func Logger() bolt.HandlerFunc {
|
||||
return func(c *bolt.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
end := time.Now()
|
||||
color := utils.Green
|
||||
m := c.Request.Method
|
||||
p := c.Request.URL.Path
|
||||
s := c.Response.Status()
|
||||
|
||||
switch {
|
||||
case s >= 500:
|
||||
color = utils.Red
|
||||
case s >= 400:
|
||||
color = utils.Yellow
|
||||
case s >= 300:
|
||||
color = utils.Cyan
|
||||
}
|
||||
|
||||
log.Printf("%s %s %s %s", m, p, color(s), end.Sub(start))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user