mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
40728e26e6
commit
4d30b193ad
6
echo.go
6
echo.go
@ -230,7 +230,7 @@ func (e *Echo) SetLogOutput(w io.Writer) {
|
||||
}
|
||||
|
||||
// SetLogLevel sets the log level for the logger. Default value is `log.FATAL`.
|
||||
func (e *Echo) SetLogLevel(l log.Level) {
|
||||
func (e *Echo) SetLogLevel(l uint8) {
|
||||
e.logger.SetLevel(l)
|
||||
}
|
||||
|
||||
@ -459,10 +459,10 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
||||
}
|
||||
|
||||
// Run starts the HTTP server.
|
||||
func (e *Echo) Run(s engine.Server) {
|
||||
func (e *Echo) Run(s engine.Server) error {
|
||||
s.SetHandler(e)
|
||||
s.SetLogger(e.logger)
|
||||
s.Start()
|
||||
return s.Start()
|
||||
}
|
||||
|
||||
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||
|
12
glide.lock
generated
12
glide.lock
generated
@ -1,5 +1,5 @@
|
||||
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
||||
updated: 2016-03-14T18:03:44.054071074-07:00
|
||||
updated: 2016-03-18T16:35:06.621226469-07:00
|
||||
imports:
|
||||
- name: github.com/klauspost/compress
|
||||
version: 006acde2c5d283d2f8b8aa03d8f0cd2891c680cf
|
||||
@ -12,7 +12,7 @@ imports:
|
||||
- name: github.com/klauspost/crc32
|
||||
version: 19b0b332c9e4516a6370a0456e6182c3b5036720
|
||||
- name: github.com/labstack/gommon
|
||||
version: c7a42f4800da9d39225ce15411f48288d622e517
|
||||
version: 81aef43cee03d40e9190983caeb70343a68d7f76
|
||||
subpackages:
|
||||
- color
|
||||
- log
|
||||
@ -21,14 +21,16 @@ imports:
|
||||
- name: github.com/mattn/go-isatty
|
||||
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
|
||||
- name: github.com/valyala/fasthttp
|
||||
version: ca2c5535a325ab129d46891de0bbe84261822b3c
|
||||
version: 2b172da53920a126cfc2532eced9400864bdacd9
|
||||
- name: github.com/valyala/fasttemplate
|
||||
version: 3b874956e03f1636d171bda64b130f9135f42cff
|
||||
- name: golang.org/x/net
|
||||
version: e7da8edaa52631091740908acaf2c2d4c9b3ce90
|
||||
version: 35b06af0720201bc2f326773a80767387544f8c4
|
||||
subpackages:
|
||||
- context
|
||||
- websocket
|
||||
- name: golang.org/x/sys
|
||||
version: 7a56174f0086b32866ebd746a794417edbc678a1
|
||||
version: 9d4e42a20653790449273b3c85e67d6d8bae6e2e
|
||||
subpackages:
|
||||
- unix
|
||||
devImports: []
|
||||
|
@ -2,10 +2,11 @@ package: github.com/labstack/echo
|
||||
import:
|
||||
- package: github.com/labstack/gommon
|
||||
subpackages:
|
||||
- /color
|
||||
- color
|
||||
- log
|
||||
- package: github.com/valyala/fasthttp
|
||||
- package: golang.org/x/net
|
||||
subpackages:
|
||||
- /context
|
||||
- context
|
||||
- websocket
|
||||
- package: github.com/valyala/fasttemplate
|
||||
|
@ -26,9 +26,9 @@ var (
|
||||
//
|
||||
// For valid credentials it calls the next handler.
|
||||
// For invalid credentials, it sends "401 - Unauthorized" response.
|
||||
func BasicAuth(fn BasicAuthFunc) echo.MiddlewareFunc {
|
||||
func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
|
||||
c := DefaultBasicAuthConfig
|
||||
c.AuthFunc = fn
|
||||
c.AuthFunc = f
|
||||
return BasicAuthFromConfig(c)
|
||||
}
|
||||
|
||||
|
@ -15,13 +15,13 @@ func TestBasicAuth(t *testing.T) {
|
||||
req := test.NewRequest(echo.GET, "/", nil)
|
||||
res := test.NewResponseRecorder()
|
||||
c := echo.NewContext(req, res, e)
|
||||
fn := func(u, p string) bool {
|
||||
f := func(u, p string) bool {
|
||||
if u == "joe" && p == "secret" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
h := BasicAuth(fn)(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h := BasicAuth(f)(echo.HandlerFunc(func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "test")
|
||||
}))
|
||||
|
||||
|
@ -1,20 +1,28 @@
|
||||
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{}
|
||||
DefaultLoggerConfig = LoggerConfig{
|
||||
Format: "[${time}] ${remote_ip} ${method} ${path} ${status} ${response_time} ${size}\n",
|
||||
}
|
||||
)
|
||||
|
||||
func Logger() echo.MiddlewareFunc {
|
||||
@ -22,13 +30,15 @@ func Logger() echo.MiddlewareFunc {
|
||||
}
|
||||
|
||||
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) error {
|
||||
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
logger := c.Logger()
|
||||
|
||||
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 != "" {
|
||||
@ -42,26 +52,46 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
||||
return err
|
||||
}
|
||||
stop := time.Now()
|
||||
method := req.Method()
|
||||
method := []byte(req.Method())
|
||||
path := req.URL().Path()
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
size := res.Size()
|
||||
took := stop.Sub(start)
|
||||
size := strconv.FormatInt(res.Size(), 10)
|
||||
|
||||
n := res.Status()
|
||||
code := color.Green(n)
|
||||
status := color.Green(n)
|
||||
switch {
|
||||
case n >= 500:
|
||||
code = color.Red(n)
|
||||
status = color.Red(n)
|
||||
case n >= 400:
|
||||
code = color.Yellow(n)
|
||||
status = color.Yellow(n)
|
||||
case n >= 300:
|
||||
code = color.Cyan(n)
|
||||
status = color.Cyan(n)
|
||||
}
|
||||
|
||||
logger.Printf("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
|
||||
return nil
|
||||
_, 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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user