mirror of
https://github.com/labstack/echo.git
synced 2025-01-07 23:01:56 +02:00
6ef5f77bf2
WIP: make default logger implemented custom writer for jsonlike logs WIP: improve examples WIP: defaultErrorHandler use errors.As to unwrap errors. Update readme WIP: default logger logs json, restore e.Start method WIP: clean router.Match a bit WIP: func types/fields have echo.Context has first element WIP: remove yaml tags as functions etc can not be serialized anyway WIP: change BindPathParams,BindQueryParams,BindHeaders from methods to functions and reverse arguments to be like DefaultBinder.Bind is WIP: improved comments, logger now extracts status from error WIP: go mod tidy WIP: rebase with 4.5.0 WIP: * removed todos. * removed StartAutoTLS and StartH2CServer methods from `StartConfig` * KeyAuth middleware errorhandler can swallow the error and resume next middleware WIP: add RouterConfig.UseEscapedPathForMatching to use escaped path for matching request against routes WIP: FIXMEs WIP: upgrade golang-jwt/jwt to `v4` WIP: refactor http methods to return RouteInfo WIP: refactor static not creating multiple routes WIP: refactor route and middleware adding functions not to return error directly WIP: Use 401 for problematic/missing headers for key auth and JWT middleware (#1552, #1402). > In summary, a 401 Unauthorized response should be used for missing or bad authentication WIP: replace `HTTPError.SetInternal` with `HTTPError.WithInternal` so we could not mutate global error variables WIP: add RouteInfo and RouteMatchType into Context what we could know from in middleware what route was matched and/or type of that match (200/404/405) WIP: make notFoundHandler and methodNotAllowedHandler private. encourage that all errors be handled in Echo.HTTPErrorHandler WIP: server cleanup ideas WIP: routable.ForGroup WIP: note about logger middleware WIP: bind should not default values on second try. use crypto rand for better randomness WIP: router add route as interface and returns info as interface WIP: improve flaky test (remains still flaky) WIP: add notes about bind default values WIP: every route can have their own path params names WIP: routerCreator and different tests WIP: different things WIP: remove route implementation WIP: support custom method types WIP: extractor tests WIP: v5.0.x proposal over v4.4.0
221 lines
5.9 KiB
Go
221 lines
5.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/valyala/fasttemplate"
|
|
)
|
|
|
|
// LoggerConfig defines the config for Logger middleware.
|
|
type LoggerConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// Tags to construct the logger format.
|
|
//
|
|
// - time_unix
|
|
// - time_unix_nano
|
|
// - time_rfc3339
|
|
// - time_rfc3339_nano
|
|
// - time_custom
|
|
// - id (Request ID)
|
|
// - remote_ip
|
|
// - uri
|
|
// - host
|
|
// - method
|
|
// - path
|
|
// - protocol
|
|
// - referer
|
|
// - user_agent
|
|
// - status
|
|
// - error
|
|
// - latency (In nanoseconds)
|
|
// - latency_human (Human readable)
|
|
// - bytes_in (Bytes received)
|
|
// - bytes_out (Bytes sent)
|
|
// - header:<NAME>
|
|
// - query:<NAME>
|
|
// - form:<NAME>
|
|
//
|
|
// Example "${remote_ip} ${status}"
|
|
//
|
|
// Optional. Default value DefaultLoggerConfig.Format.
|
|
Format string
|
|
|
|
// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
|
|
CustomTimeFormat string
|
|
|
|
// Output is a writer where logs in JSON format are written.
|
|
// Optional. Default destination `echo.Logger.Infof()`
|
|
Output io.Writer
|
|
|
|
template *fasttemplate.Template
|
|
pool *sync.Pool
|
|
}
|
|
|
|
// DefaultLoggerConfig is the default Logger middleware config.
|
|
var DefaultLoggerConfig = LoggerConfig{
|
|
Skipper: DefaultSkipper,
|
|
Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` +
|
|
`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
|
|
`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
|
|
`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n",
|
|
CustomTimeFormat: "2006-01-02 15:04:05.00000",
|
|
}
|
|
|
|
// Logger returns a middleware that logs HTTP requests.
|
|
func Logger() echo.MiddlewareFunc {
|
|
return LoggerWithConfig(DefaultLoggerConfig)
|
|
}
|
|
|
|
// LoggerWithConfig returns a Logger middleware with config or panics on invalid configuration.
|
|
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|
return toMiddlewareOrPanic(config)
|
|
}
|
|
|
|
// ToMiddleware converts LoggerConfig to middleware or returns an error for invalid configuration
|
|
func (config LoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
|
|
// Defaults
|
|
if config.Skipper == nil {
|
|
config.Skipper = DefaultLoggerConfig.Skipper
|
|
}
|
|
if config.Format == "" {
|
|
config.Format = DefaultLoggerConfig.Format
|
|
}
|
|
|
|
config.template = fasttemplate.New(config.Format, "${", "}")
|
|
config.pool = &sync.Pool{
|
|
New: func() interface{} {
|
|
return bytes.NewBuffer(make([]byte, 256))
|
|
},
|
|
}
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
if config.Skipper(c) {
|
|
return next(c)
|
|
}
|
|
|
|
req := c.Request()
|
|
res := c.Response()
|
|
|
|
start := time.Now()
|
|
err := next(c)
|
|
stop := time.Now()
|
|
|
|
buf := config.pool.Get().(*bytes.Buffer)
|
|
buf.Reset()
|
|
defer config.pool.Put(buf)
|
|
|
|
_, tmplErr := config.template.ExecuteFunc(buf, func(w io.Writer, tag string) (int, error) {
|
|
switch tag {
|
|
case "time_unix":
|
|
return buf.WriteString(strconv.FormatInt(time.Now().Unix(), 10))
|
|
case "time_unix_nano":
|
|
return buf.WriteString(strconv.FormatInt(time.Now().UnixNano(), 10))
|
|
case "time_rfc3339":
|
|
return buf.WriteString(time.Now().Format(time.RFC3339))
|
|
case "time_rfc3339_nano":
|
|
return buf.WriteString(time.Now().Format(time.RFC3339Nano))
|
|
case "time_custom":
|
|
return buf.WriteString(time.Now().Format(config.CustomTimeFormat))
|
|
case "id":
|
|
id := req.Header.Get(echo.HeaderXRequestID)
|
|
if id == "" {
|
|
id = res.Header().Get(echo.HeaderXRequestID)
|
|
}
|
|
return buf.WriteString(id)
|
|
case "remote_ip":
|
|
return buf.WriteString(c.RealIP())
|
|
case "host":
|
|
return buf.WriteString(req.Host)
|
|
case "uri":
|
|
return buf.WriteString(req.RequestURI)
|
|
case "method":
|
|
return buf.WriteString(req.Method)
|
|
case "path":
|
|
p := req.URL.Path
|
|
if p == "" {
|
|
p = "/"
|
|
}
|
|
return buf.WriteString(p)
|
|
case "protocol":
|
|
return buf.WriteString(req.Proto)
|
|
case "referer":
|
|
return buf.WriteString(req.Referer())
|
|
case "user_agent":
|
|
return buf.WriteString(req.UserAgent())
|
|
case "status":
|
|
status := res.Status
|
|
if err != nil {
|
|
if httpErr, ok := err.(*echo.HTTPError); ok {
|
|
status = httpErr.Code
|
|
}
|
|
}
|
|
return buf.WriteString(strconv.Itoa(status))
|
|
case "error":
|
|
if err != nil {
|
|
// Error may contain invalid JSON e.g. `"`
|
|
b, _ := json.Marshal(err.Error())
|
|
b = b[1 : len(b)-1]
|
|
return buf.Write(b)
|
|
}
|
|
case "latency":
|
|
l := stop.Sub(start)
|
|
return buf.WriteString(strconv.FormatInt(int64(l), 10))
|
|
case "latency_human":
|
|
return buf.WriteString(stop.Sub(start).String())
|
|
case "bytes_in":
|
|
cl := req.Header.Get(echo.HeaderContentLength)
|
|
if cl == "" {
|
|
cl = "0"
|
|
}
|
|
return buf.WriteString(cl)
|
|
case "bytes_out":
|
|
return buf.WriteString(strconv.FormatInt(res.Size, 10))
|
|
default:
|
|
switch {
|
|
case strings.HasPrefix(tag, "header:"):
|
|
return buf.Write([]byte(c.Request().Header.Get(tag[7:])))
|
|
case strings.HasPrefix(tag, "query:"):
|
|
return buf.Write([]byte(c.QueryParam(tag[6:])))
|
|
case strings.HasPrefix(tag, "form:"):
|
|
return buf.Write([]byte(c.FormValue(tag[5:])))
|
|
case strings.HasPrefix(tag, "cookie:"):
|
|
cookie, cookieErr := c.Cookie(tag[7:])
|
|
if cookieErr == nil {
|
|
return buf.Write([]byte(cookie.Value))
|
|
}
|
|
}
|
|
}
|
|
return 0, nil
|
|
})
|
|
if tmplErr != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("error in middleware chain and also failed to create log from template: %v: %w", tmplErr, err)
|
|
}
|
|
return fmt.Errorf("failed to create log from template: %w", tmplErr)
|
|
}
|
|
|
|
if config.Output != nil {
|
|
if _, lErr := config.Output.Write(buf.Bytes()); lErr != nil {
|
|
return lErr
|
|
}
|
|
} else {
|
|
if _, lErr := c.Echo().Logger.Write(buf.Bytes()); lErr != nil {
|
|
return lErr
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
}, nil
|
|
}
|