mirror of
https://github.com/labstack/echo.git
synced 2025-04-15 11:56:51 +02:00
parent
c578a662a0
commit
4f996419e1
40
echo.go
40
echo.go
@ -6,7 +6,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -16,7 +15,7 @@ import (
|
|||||||
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
|
||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/log"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
@ -35,8 +34,7 @@ type (
|
|||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
debug bool
|
debug bool
|
||||||
hook http.HandlerFunc
|
hook http.HandlerFunc
|
||||||
// stripTrailingSlash bool
|
router *Router
|
||||||
router *Router
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Route struct {
|
Route struct {
|
||||||
@ -179,6 +177,8 @@ var (
|
|||||||
methodNotAllowedHandler = func(c *Context) error {
|
methodNotAllowedHandler = func(c *Context) error {
|
||||||
return NewHTTPError(http.StatusMethodNotAllowed)
|
return NewHTTPError(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger = log.New("echo")
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates an instance of Echo.
|
// New creates an instance of Echo.
|
||||||
@ -193,9 +193,6 @@ func New() (e *Echo) {
|
|||||||
// Defaults
|
// Defaults
|
||||||
//----------
|
//----------
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
e.DisableColoredLog()
|
|
||||||
}
|
|
||||||
e.HTTP2()
|
e.HTTP2()
|
||||||
e.defaultHTTPErrorHandler = func(err error, c *Context) {
|
e.defaultHTTPErrorHandler = func(err error, c *Context) {
|
||||||
code := http.StatusInternalServerError
|
code := http.StatusInternalServerError
|
||||||
@ -210,10 +207,15 @@ func New() (e *Echo) {
|
|||||||
if !c.response.committed {
|
if !c.response.committed {
|
||||||
http.Error(c.response, msg, code)
|
http.Error(c.response, msg, code)
|
||||||
}
|
}
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
|
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
|
||||||
e.SetBinder(&binder{})
|
e.SetBinder(&binder{})
|
||||||
|
|
||||||
|
// Logger
|
||||||
|
log.SetPrefix("echo")
|
||||||
|
log.SetLevel(log.INFO)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,9 +224,14 @@ func (e *Echo) Router() *Router {
|
|||||||
return e.router
|
return e.router
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableColoredLog disables colored log.
|
// SetOutput sets the output destination for the logger.
|
||||||
func (e *Echo) DisableColoredLog() {
|
func SetOutput(w io.Writer) {
|
||||||
color.Disable()
|
log.SetOutput(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogLevel sets the log level for global logger. The default value is `log.INFO`.
|
||||||
|
func SetLogLevel(l log.Level) {
|
||||||
|
log.SetLevel(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP2 enables HTTP2 support.
|
// HTTP2 enables HTTP2 support.
|
||||||
@ -269,11 +276,6 @@ func (e *Echo) Hook(h http.HandlerFunc) {
|
|||||||
e.hook = h
|
e.hook = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// StripTrailingSlash enables removing trailing slash from the request path.
|
|
||||||
// func (e *Echo) StripTrailingSlash() {
|
|
||||||
// e.stripTrailingSlash = true
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Use adds handler to the middleware chain.
|
// Use adds handler to the middleware chain.
|
||||||
func (e *Echo) Use(m ...Middleware) {
|
func (e *Echo) Use(m ...Middleware) {
|
||||||
for _, h := range m {
|
for _, h := range m {
|
||||||
@ -533,7 +535,7 @@ func (e *Echo) run(s *http.Server, files ...string) {
|
|||||||
} else if len(files) == 2 {
|
} else if len(files) == 2 {
|
||||||
log.Fatal(s.ListenAndServeTLS(files[0], files[1]))
|
log.Fatal(s.ListenAndServeTLS(files[0], files[1]))
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("echo => invalid TLS configuration")
|
log.Fatal("invalid TLS configuration")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,7 +590,7 @@ func wrapMiddleware(m Middleware) MiddlewareFunc {
|
|||||||
case func(http.ResponseWriter, *http.Request):
|
case func(http.ResponseWriter, *http.Request):
|
||||||
return wrapHTTPHandlerFuncMW(m)
|
return wrapHTTPHandlerFuncMW(m)
|
||||||
default:
|
default:
|
||||||
panic("echo => unknown middleware")
|
panic("unknown middleware")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -634,7 +636,7 @@ func wrapHandler(h Handler) HandlerFunc {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic("echo => unknown handler")
|
panic("unknown handler")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/color"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Logger() echo.MiddlewareFunc {
|
func Logger() echo.MiddlewareFunc {
|
||||||
@ -47,7 +47,7 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
code = color.Cyan(n)
|
code = color.Cyan(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
|
log.Info("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,7 +54,6 @@ func TestLogger(t *testing.T) {
|
|||||||
func TestLoggerIPAddress(t *testing.T) {
|
func TestLoggerIPAddress(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
log.SetOutput(buf)
|
log.SetOutput(buf)
|
||||||
|
|
||||||
ip := "127.0.0.1"
|
ip := "127.0.0.1"
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
@ -18,7 +18,7 @@ func Recover() echo.MiddlewareFunc {
|
|||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
trace := make([]byte, 1<<16)
|
trace := make([]byte, 1<<16)
|
||||||
n := runtime.Stack(trace, true)
|
n := runtime.Stack(trace, true)
|
||||||
c.Error(fmt.Errorf("echo => panic recover\n %v\n stack trace %d bytes\n %s",
|
c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s",
|
||||||
err, n, trace[:n]))
|
err, n, trace[:n]))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -2,11 +2,10 @@ package echo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -36,8 +35,7 @@ func (r *Response) Writer() http.ResponseWriter {
|
|||||||
|
|
||||||
func (r *Response) WriteHeader(code int) {
|
func (r *Response) WriteHeader(code int) {
|
||||||
if r.committed {
|
if r.committed {
|
||||||
// TODO: Warning
|
log.Warn("response already committed")
|
||||||
log.Printf("echo => %s", color.Yellow("response already committed"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.status = code
|
r.status = code
|
||||||
|
@ -25,9 +25,17 @@ and message `HTTPError.Message`.
|
|||||||
|
|
||||||
Enables/disables debug mode.
|
Enables/disables debug mode.
|
||||||
|
|
||||||
### Disable colored log
|
### Log output
|
||||||
|
|
||||||
`Echo#DisableColoredLog()`
|
`echo#SetOutput(w io.Writer)`
|
||||||
|
|
||||||
|
SetOutput sets the output destination for the global logger.
|
||||||
|
|
||||||
|
### Log level
|
||||||
|
|
||||||
|
`echo#SetLogLevel(l log.Level)`
|
||||||
|
|
||||||
|
SetLogLevel sets the log level for global logger. The default value is `log.INFO`.
|
||||||
|
|
||||||
### Hook
|
### Hook
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ menu:
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
e.Use(func(c *echo.Context) error {
|
e.Use(func(c *echo.Context) error {
|
||||||
log.Println(c.Path()) // Prints `/users/:name`
|
println(c.Path()) // Prints `/users/:name`
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
e.Get("/users/:name", func(c *echo.Context) error) {
|
e.Get("/users/:name", func(c *echo.Context) error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user