1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00
echo/middleware/logger.go

98 lines
2.1 KiB
Go
Raw Normal View History

2015-04-23 10:44:50 +02:00
package middleware
import (
"fmt"
"io"
"net"
"strconv"
"time"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
"github.com/valyala/fasttemplate"
)
type (
LoggerConfig struct {
Format string
template *fasttemplate.Template
}
)
var (
DefaultLoggerConfig = LoggerConfig{
Format: "[${time}] ${remote_ip} ${method} ${path} ${status} ${response_time} ${size}\n",
}
)
func Logger() echo.MiddlewareFunc {
return LoggerFromConfig(DefaultLoggerConfig)
}
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
config.template = fasttemplate.New(config.Format, "${", "}")
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
remoteAddr := req.RemoteAddress()
output := c.Logger().Output()
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()
if err := next.Handle(c); err != nil {
return err
}
stop := time.Now()
method := []byte(req.Method())
path := req.URL().Path()
if path == "" {
path = "/"
}
took := stop.Sub(start)
size := strconv.FormatInt(res.Size(), 10)
n := res.Status()
status := color.Green(n)
switch {
case n >= 500:
status = color.Red(n)
case n >= 400:
status = color.Yellow(n)
case n >= 300:
status = color.Cyan(n)
}
_, 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
})
}
}