1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-17 21:08:05 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-11-20 19:13:22 -08:00
parent c578a662a0
commit 4f996419e1
7 changed files with 38 additions and 31 deletions

40
echo.go
View File

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"path/filepath"
"reflect"
@ -16,7 +15,7 @@ import (
"encoding/xml"
"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
"golang.org/x/net/http2"
"golang.org/x/net/websocket"
)
@ -35,8 +34,7 @@ type (
pool sync.Pool
debug bool
hook http.HandlerFunc
// stripTrailingSlash bool
router *Router
router *Router
}
Route struct {
@ -179,6 +177,8 @@ var (
methodNotAllowedHandler = func(c *Context) error {
return NewHTTPError(http.StatusMethodNotAllowed)
}
logger = log.New("echo")
)
// New creates an instance of Echo.
@ -193,9 +193,6 @@ func New() (e *Echo) {
// Defaults
//----------
if runtime.GOOS == "windows" {
e.DisableColoredLog()
}
e.HTTP2()
e.defaultHTTPErrorHandler = func(err error, c *Context) {
code := http.StatusInternalServerError
@ -210,10 +207,15 @@ func New() (e *Echo) {
if !c.response.committed {
http.Error(c.response, msg, code)
}
log.Println(err)
log.Error(err)
}
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(&binder{})
// Logger
log.SetPrefix("echo")
log.SetLevel(log.INFO)
return
}
@ -222,9 +224,14 @@ func (e *Echo) Router() *Router {
return e.router
}
// DisableColoredLog disables colored log.
func (e *Echo) DisableColoredLog() {
color.Disable()
// SetOutput sets the output destination for the logger.
func SetOutput(w io.Writer) {
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.
@ -269,11 +276,6 @@ func (e *Echo) Hook(h http.HandlerFunc) {
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.
func (e *Echo) Use(m ...Middleware) {
for _, h := range m {
@ -533,7 +535,7 @@ func (e *Echo) run(s *http.Server, files ...string) {
} else if len(files) == 2 {
log.Fatal(s.ListenAndServeTLS(files[0], files[1]))
} 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):
return wrapHTTPHandlerFuncMW(m)
default:
panic("echo => unknown middleware")
panic("unknown middleware")
}
}
@ -634,7 +636,7 @@ func wrapHandler(h Handler) HandlerFunc {
return nil
}
default:
panic("echo => unknown handler")
panic("unknown handler")
}
}

View File

@ -1,12 +1,12 @@
package middleware
import (
"log"
"net"
"time"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
)
func Logger() echo.MiddlewareFunc {
@ -47,7 +47,7 @@ func Logger() echo.MiddlewareFunc {
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
}
}

View File

@ -3,12 +3,12 @@ package middleware
import (
"bytes"
"errors"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
"github.com/stretchr/testify/assert"
)
@ -54,7 +54,6 @@ func TestLogger(t *testing.T) {
func TestLoggerIPAddress(t *testing.T) {
buf := &bytes.Buffer{}
log.SetOutput(buf)
ip := "127.0.0.1"
e := echo.New()

View File

@ -18,7 +18,7 @@ func Recover() echo.MiddlewareFunc {
if err := recover(); err != nil {
trace := make([]byte, 1<<16)
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]))
}
}()

View File

@ -2,11 +2,10 @@ package echo
import (
"bufio"
"log"
"net"
"net/http"
"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
)
type (
@ -36,8 +35,7 @@ func (r *Response) Writer() http.ResponseWriter {
func (r *Response) WriteHeader(code int) {
if r.committed {
// TODO: Warning
log.Printf("echo => %s", color.Yellow("response already committed"))
log.Warn("response already committed")
return
}
r.status = code

View File

@ -25,9 +25,17 @@ and message `HTTPError.Message`.
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

View File

@ -14,7 +14,7 @@ menu:
```go
e.Use(func(c *echo.Context) error {
log.Println(c.Path()) // Prints `/users/:name`
println(c.Path()) // Prints `/users/:name`
return nil
})
e.Get("/users/:name", func(c *echo.Context) error) {