Skip to content

Commit

Permalink
Fixed #255
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Nov 21, 2015
1 parent c578a66 commit dc635e1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
40 changes: 21 additions & 19 deletions echo.go
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"path/filepath"
"reflect"
Expand All @@ -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"
)
Expand All @@ -35,8 +34,7 @@ type (
pool sync.Pool
debug bool
hook http.HandlerFunc
// stripTrailingSlash bool
router *Router
router *Router
}

Route struct {
Expand Down Expand Up @@ -179,6 +177,8 @@ var (
methodNotAllowedHandler = func(c *Context) error {
return NewHTTPError(http.StatusMethodNotAllowed)
}

logger = log.New("echo")
)

// New creates an instance of Echo.
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -634,7 +636,7 @@ func wrapHandler(h Handler) HandlerFunc {
return nil
}
default:
panic("echo => unknown handler")
panic("unknown handler")
}
}

Expand Down
4 changes: 2 additions & 2 deletions middleware/logger.go
@@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
3 changes: 1 addition & 2 deletions middleware/logger_test.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion middleware/recover.go
Expand Up @@ -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]))
}
}()
Expand Down
6 changes: 2 additions & 4 deletions response.go
Expand Up @@ -2,11 +2,10 @@ package echo

import (
"bufio"
"log"
"net"
"net/http"

"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
)

type (
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions website/content/guide/customization.md
Expand Up @@ -25,9 +25,11 @@ 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.

### Hook

Expand Down
2 changes: 1 addition & 1 deletion website/content/guide/request.md
Expand Up @@ -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) {
Expand Down

0 comments on commit dc635e1

Please sign in to comment.