Skip to content

Commit

Permalink
Moved logger to root
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Sep 25, 2016
1 parent 335b1d0 commit 0349e88
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
6 changes: 2 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

"golang.org/x/net/websocket"

"github.com/labstack/echo/log"

"bytes"

"github.com/labstack/echo/context"
Expand Down Expand Up @@ -185,7 +183,7 @@ type (
SetHandler(HandlerFunc)

// Logger returns the `Logger` instance.
Logger() log.Logger
Logger() Logger

// Echo returns the `Echo` instance.
Echo() *Echo
Expand Down Expand Up @@ -560,7 +558,7 @@ func (c *echoContext) SetHandler(h HandlerFunc) {
c.handler = h
}

func (c *echoContext) Logger() log.Logger {
func (c *echoContext) Logger() Logger {
return c.echo.Logger
}

Expand Down
35 changes: 17 additions & 18 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import (
"golang.org/x/net/context"
"golang.org/x/net/websocket"

"github.com/labstack/echo/log"
"github.com/labstack/gommon/color"
glog "github.com/labstack/gommon/log"
"github.com/tylerb/graceful"
Expand All @@ -72,7 +71,7 @@ type (
HTTPErrorHandler
Binder Binder
Renderer Renderer
Logger log.Logger
Logger Logger
graceful *graceful.Server
gracefulTLS *graceful.Server
premiddleware []MiddlewareFunc
Expand Down Expand Up @@ -239,21 +238,17 @@ func New() (e *Echo) {
TLSConfig: new(tls.Config),
ShutdownTimeout: 15 * time.Second,
Logger: glog.New("echo"),
maxParam: new(int),
graceful: new(graceful.Server),
gracefulTLS: new(graceful.Server),
maxParam: new(int),
}
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
e.Binder = &binder{}
e.Logger.SetLevel(glog.OFF)
e.graceful = &graceful.Server{
Timeout: e.ShutdownTimeout,
Logger: slog.New(e.Logger.Output(), "echo: ", 0),
}
*e.gracefulTLS = *e.graceful
e.pool.New = func() interface{} {
return e.NewContext(nil, nil)
}
e.router = NewRouter(e)
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
e.Binder = &binder{}
return
}

Expand Down Expand Up @@ -510,22 +505,26 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.pool.Put(c)
}

func (e *Echo) configGraceful(gs *graceful.Server, s *http.Server, address string) {
gs.Server = s
gs.Addr = address
gs.Handler = e
gs.Timeout = e.ShutdownTimeout
gs.Logger = slog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
}

// Start starts the HTTP server.
// Note: If custom `http.Server` is used, it's Addr and Handler properties are ignored.
// Note: If custom `Echo#Server` is used, it's Addr and Handler properties are ignored.
func (e *Echo) Start(address string) (err error) {
e.Server.Handler = e
e.graceful.Server = e.Server
e.graceful.Addr = address
e.configGraceful(e.graceful, e.Server, address)
color.Printf(" ⇛ http server started on %s\n", color.Green(address))
return e.graceful.ListenAndServe()
}

// StartTLS starts the TLS server.
// Note: If custom `http.Server` is used, it's Addr and Handler properties are ignored.
// Note: If custom `Echo#TLSServer` is used, it's Addr and Handler properties are ignored.
func (e *Echo) StartTLS(address string, certFile, keyFile string) (err error) {
e.TLSServer.Handler = e
e.gracefulTLS.Server = e.TLSServer
e.gracefulTLS.Addr = address
e.configGraceful(e.gracefulTLS, e.TLSServer, address)
if certFile == "" || keyFile == "" {
return errors.New("invalid tls configuration")
}
Expand Down
4 changes: 2 additions & 2 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func TestEchoContext(t *testing.T) {
func TestEchoStart(t *testing.T) {
e := New()
go func() {
assert.NoError(t, e.Start(":1323"))
assert.NoError(t, e.Start(":0"))
}()
time.Sleep(200 * time.Millisecond)
e.Shutdown(1 * time.Second)
Expand All @@ -420,7 +420,7 @@ func TestEchoStart(t *testing.T) {
func TestEchoStartTLS(t *testing.T) {
e := New()
go func() {
assert.NoError(t, e.StartTLS(":1323", "_fixture/certs/cert.pem", "_fixture/certs/key.pem"))
assert.NoError(t, e.StartTLS(":0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem"))
}()
time.Sleep(200 * time.Millisecond)
e.ShutdownTLS(1 * time.Second)
Expand Down
4 changes: 3 additions & 1 deletion log/logger.go → logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package echo

import (
"io"
Expand All @@ -13,6 +13,8 @@ type (
SetOutput(io.Writer)
Level() log.Lvl
SetLevel(log.Lvl)
Prefix() string
SetPrefix(string)
Print(...interface{})
Printf(string, ...interface{})
Printj(log.JSON)
Expand Down

0 comments on commit 0349e88

Please sign in to comment.