2015-04-23 10:44:50 +02:00
|
|
|
package middleware
|
2015-04-21 08:24:57 +02:00
|
|
|
|
|
|
|
import (
|
2016-03-19 05:02:02 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-18 01:17:54 +02:00
|
|
|
"net"
|
2016-03-19 05:02:02 +02:00
|
|
|
"strconv"
|
2015-04-21 08:24:57 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/gommon/color"
|
2016-03-19 05:02:02 +02:00
|
|
|
"github.com/valyala/fasttemplate"
|
2015-04-21 08:24:57 +02:00
|
|
|
)
|
|
|
|
|
2016-02-18 07:01:47 +02:00
|
|
|
type (
|
2016-03-14 22:55:38 +02:00
|
|
|
LoggerConfig struct {
|
2016-03-19 05:02:02 +02:00
|
|
|
Format string
|
|
|
|
template *fasttemplate.Template
|
2015-04-21 08:24:57 +02:00
|
|
|
}
|
2016-02-18 07:01:47 +02:00
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-03-19 05:02:02 +02:00
|
|
|
DefaultLoggerConfig = LoggerConfig{
|
|
|
|
Format: "[${time}] ${remote_ip} ${method} ${path} ${status} ${response_time} ${size}\n",
|
|
|
|
}
|
2016-03-14 22:55:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Logger() echo.MiddlewareFunc {
|
|
|
|
return LoggerFromConfig(DefaultLoggerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
2016-03-19 05:02:02 +02:00
|
|
|
config.template = fasttemplate.New(config.Format, "${", "}")
|
|
|
|
|
2016-02-19 08:50:40 +02:00
|
|
|
return func(next echo.Handler) echo.Handler {
|
2016-03-19 05:02:02 +02:00
|
|
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
2016-02-18 07:49:31 +02:00
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
|
|
|
remoteAddr := req.RemoteAddress()
|
2016-03-19 05:02:02 +02:00
|
|
|
output := c.Logger().Output()
|
|
|
|
|
2016-02-18 07:49:31 +02:00
|
|
|
if ip := req.Header().Get(echo.XRealIP); ip != "" {
|
|
|
|
remoteAddr = ip
|
|
|
|
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
|
|
|
|
remoteAddr = ip
|
|
|
|
} else {
|
|
|
|
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
2016-02-19 08:50:40 +02:00
|
|
|
if err := next.Handle(c); err != nil {
|
2016-03-17 22:24:52 +02:00
|
|
|
return err
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
|
|
|
stop := time.Now()
|
2016-03-19 05:02:02 +02:00
|
|
|
method := []byte(req.Method())
|
2016-02-18 07:49:31 +02:00
|
|
|
path := req.URL().Path()
|
|
|
|
if path == "" {
|
|
|
|
path = "/"
|
|
|
|
}
|
2016-03-19 05:02:02 +02:00
|
|
|
took := stop.Sub(start)
|
|
|
|
size := strconv.FormatInt(res.Size(), 10)
|
2016-02-18 07:49:31 +02:00
|
|
|
|
|
|
|
n := res.Status()
|
2016-03-19 05:02:02 +02:00
|
|
|
status := color.Green(n)
|
2016-02-18 07:49:31 +02:00
|
|
|
switch {
|
|
|
|
case n >= 500:
|
2016-03-19 05:02:02 +02:00
|
|
|
status = color.Red(n)
|
2016-02-18 07:49:31 +02:00
|
|
|
case n >= 400:
|
2016-03-19 05:02:02 +02:00
|
|
|
status = color.Yellow(n)
|
2016-02-18 07:49:31 +02:00
|
|
|
case n >= 300:
|
2016-03-19 05:02:02 +02:00
|
|
|
status = color.Cyan(n)
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
|
|
|
|
2016-03-19 05:02:02 +02:00
|
|
|
_, err = config.template.ExecuteFunc(output, func(w io.Writer, tag string) (int, error) {
|
|
|
|
switch tag {
|
|
|
|
case "time":
|
|
|
|
return w.Write([]byte(time.Now().Format(time.Stamp)))
|
|
|
|
case "remote_ip":
|
|
|
|
return w.Write([]byte(remoteAddr))
|
|
|
|
case "method":
|
|
|
|
return w.Write(method)
|
|
|
|
case "path":
|
|
|
|
return w.Write([]byte(path))
|
|
|
|
case "status":
|
|
|
|
return w.Write([]byte(status))
|
|
|
|
case "response_time":
|
|
|
|
return w.Write([]byte(took.String()))
|
|
|
|
case "size":
|
|
|
|
return w.Write([]byte(size))
|
|
|
|
default:
|
|
|
|
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
2016-02-18 07:49:31 +02:00
|
|
|
})
|
|
|
|
}
|
2015-04-21 08:24:57 +02:00
|
|
|
}
|