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 07:02:10 +02:00
|
|
|
"os"
|
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 07:02:10 +02:00
|
|
|
"github.com/mattn/go-isatty"
|
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-20 00:47:20 +02:00
|
|
|
// LoggerConfig defines config for logger middleware.
|
|
|
|
//
|
2016-03-14 22:55:38 +02:00
|
|
|
LoggerConfig struct {
|
2016-03-23 05:56:29 +02:00
|
|
|
// Format is the log format which can be constructed using the following tags:
|
2016-03-20 00:47:20 +02:00
|
|
|
//
|
|
|
|
// - time_rfc3339
|
|
|
|
// - remote_ip
|
2016-03-22 02:27:14 +02:00
|
|
|
// - uri
|
2016-03-20 00:47:20 +02:00
|
|
|
// - method
|
|
|
|
// - path
|
|
|
|
// - status
|
|
|
|
// - response_time
|
|
|
|
// - response_size
|
2016-03-23 05:56:29 +02:00
|
|
|
//
|
|
|
|
// Example "${remote_id} ${status}"
|
2016-03-24 05:56:28 +02:00
|
|
|
Format string
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-03-23 05:56:29 +02:00
|
|
|
// Output is the writer where logs are written.
|
2016-03-24 05:56:28 +02:00
|
|
|
Output io.Writer
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-03-19 05:02:02 +02:00
|
|
|
template *fasttemplate.Template
|
2016-03-19 07:02:10 +02:00
|
|
|
color *color.Color
|
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-20 00:47:20 +02:00
|
|
|
// DefaultLoggerConfig is the default logger middleware config.
|
2016-03-19 05:02:02 +02:00
|
|
|
DefaultLoggerConfig = LoggerConfig{
|
2016-03-22 02:27:14 +02:00
|
|
|
Format: "time=${time_rfc3339}, remote_ip=${remote_ip}, method=${method}, " +
|
2016-03-23 05:56:29 +02:00
|
|
|
"uri=${uri}, status=${status}, took=${response_time}, sent=${response_size} bytes\n",
|
2016-03-19 07:02:10 +02:00
|
|
|
color: color.New(),
|
|
|
|
Output: os.Stdout,
|
2016-03-19 05:02:02 +02:00
|
|
|
}
|
2016-03-14 22:55:38 +02:00
|
|
|
)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Logger returns a middleware that logs HTTP requests.
|
2016-03-14 22:55:38 +02:00
|
|
|
func Logger() echo.MiddlewareFunc {
|
|
|
|
return LoggerFromConfig(DefaultLoggerConfig)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// LoggerFromConfig returns a logger middleware from config.
|
|
|
|
// See `Logger()`.
|
2016-03-14 22:55:38 +02:00
|
|
|
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
2016-03-19 05:02:02 +02:00
|
|
|
config.template = fasttemplate.New(config.Format, "${", "}")
|
2016-03-19 07:02:10 +02:00
|
|
|
config.color = color.New()
|
|
|
|
if w, ok := config.Output.(*os.File); ok && !isatty.IsTerminal(w.Fd()) {
|
|
|
|
config.color.Disable()
|
|
|
|
}
|
2016-03-19 05:02:02 +02:00
|
|
|
|
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-03-22 02:27:14 +02:00
|
|
|
rq := c.Request()
|
|
|
|
rs := c.Response()
|
2016-02-18 07:49:31 +02:00
|
|
|
start := time.Now()
|
2016-03-23 05:56:29 +02:00
|
|
|
if err = next.Handle(c); err != nil {
|
2016-03-20 17:14:55 +02:00
|
|
|
c.Error(err)
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
|
2016-03-19 07:02:10 +02:00
|
|
|
_, err = config.template.ExecuteFunc(config.Output, func(w io.Writer, tag string) (int, error) {
|
2016-03-19 05:02:02 +02:00
|
|
|
switch tag {
|
2016-03-19 05:29:53 +02:00
|
|
|
case "time_rfc3339":
|
2016-03-19 05:40:41 +02:00
|
|
|
return w.Write([]byte(time.Now().Format(time.RFC3339)))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "remote_ip":
|
2016-03-22 02:27:14 +02:00
|
|
|
ra := rq.RemoteAddress()
|
|
|
|
if ip := rq.Header().Get(echo.XRealIP); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else if ip = rq.Header().Get(echo.XForwardedFor); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else {
|
|
|
|
ra, _, _ = net.SplitHostPort(ra)
|
|
|
|
}
|
|
|
|
return w.Write([]byte(ra))
|
|
|
|
case "uri":
|
|
|
|
return w.Write([]byte(rq.URI()))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "method":
|
2016-03-22 02:27:14 +02:00
|
|
|
return w.Write([]byte(rq.Method()))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "path":
|
2016-03-22 02:27:14 +02:00
|
|
|
p := rq.URL().Path()
|
|
|
|
if p == "" {
|
|
|
|
p = "/"
|
|
|
|
}
|
|
|
|
return w.Write([]byte(p))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "status":
|
2016-03-22 02:27:14 +02:00
|
|
|
n := rs.Status()
|
|
|
|
s := color.Green(n)
|
|
|
|
switch {
|
|
|
|
case n >= 500:
|
|
|
|
s = color.Red(n)
|
|
|
|
case n >= 400:
|
|
|
|
s = color.Yellow(n)
|
|
|
|
case n >= 300:
|
|
|
|
s = color.Cyan(n)
|
|
|
|
}
|
|
|
|
return w.Write([]byte(s))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "response_time":
|
2016-03-22 02:27:14 +02:00
|
|
|
return w.Write([]byte(stop.Sub(start).String()))
|
2016-03-20 00:47:20 +02:00
|
|
|
case "response_size":
|
2016-03-22 02:27:14 +02:00
|
|
|
return w.Write([]byte(strconv.FormatInt(rs.Size(), 10)))
|
2016-03-19 05:02:02 +02:00
|
|
|
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
|
|
|
}
|