2015-04-23 10:44:50 +02:00
|
|
|
package middleware
|
2015-04-21 08:24:57 +02:00
|
|
|
|
|
|
|
import (
|
2016-04-22 13:05:49 +02:00
|
|
|
"bytes"
|
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"
|
2016-04-22 13:05:49 +02:00
|
|
|
"sync"
|
2015-04-21 08:24:57 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/gommon/color"
|
2016-04-01 01:30:19 +02:00
|
|
|
isatty "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-04-01 01:30:19 +02:00
|
|
|
// LoggerConfig defines the 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
|
2016-05-10 16:57:35 +02:00
|
|
|
// - id (Request ID - Not implemented)
|
2016-03-20 00:47:20 +02:00
|
|
|
// - remote_ip
|
2016-03-22 02:27:14 +02:00
|
|
|
// - uri
|
2016-05-10 16:57:35 +02:00
|
|
|
// - host
|
2016-03-20 00:47:20 +02:00
|
|
|
// - method
|
|
|
|
// - path
|
2016-05-10 16:57:35 +02:00
|
|
|
// - referer
|
|
|
|
// - user_agent
|
2016-03-20 00:47:20 +02:00
|
|
|
// - status
|
2016-05-10 16:57:35 +02:00
|
|
|
// - latency (In microseconds)
|
|
|
|
// - latency_human (Human readable)
|
|
|
|
// - rx_bytes (Bytes received)
|
|
|
|
// - tx_bytes (Bytes sent)
|
2016-03-23 05:56:29 +02:00
|
|
|
//
|
2016-05-10 16:57:35 +02:00
|
|
|
// Example "${remote_ip} ${status}"
|
2016-04-01 01:30:19 +02:00
|
|
|
//
|
2016-04-20 16:32:51 +02:00
|
|
|
// Optional, with default value as `DefaultLoggerConfig.Format`.
|
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-04-06 03:57:57 +02:00
|
|
|
// Optional with default value as os.Stdout.
|
2016-03-24 05:56:28 +02:00
|
|
|
Output io.Writer
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
template *fasttemplate.Template
|
|
|
|
color *color.Color
|
2016-04-22 13:05:49 +02:00
|
|
|
bufferPool sync.Pool
|
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-05-10 16:57:35 +02:00
|
|
|
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
|
|
|
`"latency_human":"${latency_human}","rx_bytes":${rx_bytes},` +
|
|
|
|
`"tx_bytes":${tx_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 {
|
2016-04-08 06:20:50 +02:00
|
|
|
return LoggerWithConfig(DefaultLoggerConfig)
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 06:20:50 +02:00
|
|
|
// LoggerWithConfig returns a logger middleware from config.
|
2016-03-20 00:47:20 +02:00
|
|
|
// See `Logger()`.
|
2016-04-08 06:20:50 +02:00
|
|
|
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
2016-04-01 01:30:19 +02:00
|
|
|
// Defaults
|
|
|
|
if config.Format == "" {
|
|
|
|
config.Format = DefaultLoggerConfig.Format
|
|
|
|
}
|
|
|
|
if config.Output == nil {
|
|
|
|
config.Output = DefaultLoggerConfig.Output
|
|
|
|
}
|
|
|
|
|
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()
|
2016-04-11 20:47:54 +02:00
|
|
|
if w, ok := config.Output.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
|
2016-03-19 07:02:10 +02:00
|
|
|
config.color.Disable()
|
|
|
|
}
|
2016-04-22 13:05:49 +02:00
|
|
|
config.bufferPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return bytes.NewBuffer(make([]byte, 256))
|
|
|
|
},
|
|
|
|
}
|
2016-03-19 05:02:02 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) (err error) {
|
2016-04-24 19:21:23 +02:00
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
2016-02-18 07:49:31 +02:00
|
|
|
start := time.Now()
|
2016-04-02 23:19:39 +02:00
|
|
|
if err = next(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-04-24 19:21:23 +02:00
|
|
|
buf := config.bufferPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer config.bufferPool.Put(buf)
|
2016-02-18 07:49:31 +02:00
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
_, err = config.template.ExecuteFunc(buf, 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-04-24 19:21:23 +02:00
|
|
|
ra := req.RemoteAddress()
|
|
|
|
if ip := req.Header().Get(echo.HeaderXRealIP); ip != "" {
|
2016-03-22 02:27:14 +02:00
|
|
|
ra = ip
|
2016-04-24 19:21:23 +02:00
|
|
|
} else if ip = req.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
2016-03-22 02:27:14 +02:00
|
|
|
ra = ip
|
|
|
|
} else {
|
|
|
|
ra, _, _ = net.SplitHostPort(ra)
|
|
|
|
}
|
|
|
|
return w.Write([]byte(ra))
|
2016-05-10 16:57:35 +02:00
|
|
|
case "host":
|
|
|
|
return w.Write([]byte(req.Host()))
|
2016-03-22 02:27:14 +02:00
|
|
|
case "uri":
|
2016-04-24 19:21:23 +02:00
|
|
|
return w.Write([]byte(req.URI()))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "method":
|
2016-04-24 19:21:23 +02:00
|
|
|
return w.Write([]byte(req.Method()))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "path":
|
2016-04-24 19:21:23 +02:00
|
|
|
p := req.URL().Path()
|
2016-03-22 02:27:14 +02:00
|
|
|
if p == "" {
|
|
|
|
p = "/"
|
|
|
|
}
|
|
|
|
return w.Write([]byte(p))
|
2016-05-10 16:57:35 +02:00
|
|
|
case "referer":
|
|
|
|
return w.Write([]byte(req.Referer()))
|
|
|
|
case "user_agent":
|
|
|
|
return w.Write([]byte(req.UserAgent()))
|
2016-03-19 05:02:02 +02:00
|
|
|
case "status":
|
2016-04-24 19:21:23 +02:00
|
|
|
n := res.Status()
|
2016-04-11 20:47:54 +02:00
|
|
|
s := config.color.Green(n)
|
2016-03-22 02:27:14 +02:00
|
|
|
switch {
|
|
|
|
case n >= 500:
|
2016-04-11 20:47:54 +02:00
|
|
|
s = config.color.Red(n)
|
2016-03-22 02:27:14 +02:00
|
|
|
case n >= 400:
|
2016-04-11 20:47:54 +02:00
|
|
|
s = config.color.Yellow(n)
|
2016-03-22 02:27:14 +02:00
|
|
|
case n >= 300:
|
2016-04-11 20:47:54 +02:00
|
|
|
s = config.color.Cyan(n)
|
2016-03-22 02:27:14 +02:00
|
|
|
}
|
|
|
|
return w.Write([]byte(s))
|
2016-05-10 16:57:35 +02:00
|
|
|
case "latency":
|
|
|
|
l := stop.Sub(start).Nanoseconds() / 1000
|
|
|
|
return w.Write([]byte(strconv.FormatInt(l, 10)))
|
|
|
|
case "latency_human":
|
2016-03-22 02:27:14 +02:00
|
|
|
return w.Write([]byte(stop.Sub(start).String()))
|
2016-05-10 16:57:35 +02:00
|
|
|
case "rx_bytes":
|
|
|
|
b := req.Header().Get(echo.HeaderContentLength)
|
|
|
|
if b == "" {
|
|
|
|
b = "0"
|
|
|
|
}
|
|
|
|
return w.Write([]byte(b))
|
|
|
|
case "tx_bytes":
|
2016-04-24 19:21:23 +02:00
|
|
|
return w.Write([]byte(strconv.FormatInt(res.Size(), 10)))
|
2016-03-19 05:02:02 +02:00
|
|
|
default:
|
|
|
|
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))
|
|
|
|
}
|
|
|
|
})
|
2016-04-22 13:05:49 +02:00
|
|
|
if err == nil {
|
2016-04-24 19:21:23 +02:00
|
|
|
config.Output.Write(buf.Bytes())
|
2016-04-22 13:05:49 +02:00
|
|
|
}
|
2016-03-19 05:02:02 +02:00
|
|
|
return
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2015-04-21 08:24:57 +02:00
|
|
|
}
|