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
|
|
|
"io"
|
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-07-27 18:34:44 +02:00
|
|
|
// LoggerConfig defines the config for Logger middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
LoggerConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// 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)
|
2016-07-27 18:34:44 +02:00
|
|
|
// - bytes_in (Bytes received)
|
|
|
|
// - bytes_out (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-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value DefaultLoggerConfig.Format.
|
2016-05-19 03:53:54 +02:00
|
|
|
Format string `json:"format"`
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Output is a writer where logs are written.
|
|
|
|
// Optional. Default value 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-07-27 18:34:44 +02:00
|
|
|
// DefaultLoggerConfig is the default Logger middleware config.
|
2016-03-19 05:02:02 +02:00
|
|
|
DefaultLoggerConfig = LoggerConfig{
|
2016-07-27 18:34:44 +02:00
|
|
|
Skipper: defaultSkipper,
|
2016-05-10 16:57:35 +02:00
|
|
|
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
2016-07-27 18:34:44 +02:00
|
|
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
|
|
|
|
`"bytes_out":${bytes_out}}` + "\n",
|
2016-03-19 07:02:10 +02:00
|
|
|
Output: os.Stdout,
|
2016-07-27 18:34:44 +02:00
|
|
|
color: color.New(),
|
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-09-01 05:10:14 +02:00
|
|
|
// LoggerWithConfig returns a Logger middleware with config.
|
2016-05-13 02:45:00 +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
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultLoggerConfig.Skipper
|
|
|
|
}
|
2016-04-01 01:30:19 +02:00
|
|
|
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-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
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-08-18 03:26:58 +02:00
|
|
|
ra := req.RealIP()
|
2016-03-22 02:27:14 +02:00
|
|
|
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-07-27 18:34:44 +02:00
|
|
|
case "bytes_in":
|
2016-05-10 16:57:35 +02:00
|
|
|
b := req.Header().Get(echo.HeaderContentLength)
|
|
|
|
if b == "" {
|
|
|
|
b = "0"
|
|
|
|
}
|
|
|
|
return w.Write([]byte(b))
|
2016-07-27 18:34:44 +02:00
|
|
|
case "bytes_out":
|
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
|
|
|
}
|
2016-06-09 02:16:03 +02:00
|
|
|
return 0, nil
|
2016-03-19 05:02:02 +02:00
|
|
|
})
|
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
|
|
|
}
|