1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00

Merge pull request #349 from o1egl/logger_as_interface_v1

Replace real object logger with interface.
This commit is contained in:
Vishal Rana 2016-01-31 00:17:43 -08:00
commit 5ffc118054
2 changed files with 28 additions and 18 deletions

39
echo.go
View File

@ -66,10 +66,28 @@ type (
debug bool debug bool
hook http.HandlerFunc hook http.HandlerFunc
autoIndex bool autoIndex bool
logger *log.Logger logger Logger
router *Router router *Router
} }
// Logger is the interface that declares echo's logging system.
Logger interface {
Debug(...interface{})
Debugf(string, ...interface{})
Info(...interface{})
Infof(string, ...interface{})
Warn(...interface{})
Warnf(string, ...interface{})
Error(...interface{})
Errorf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
}
// Route contains a handler and information for matching against requests. // Route contains a handler and information for matching against requests.
Route struct { Route struct {
Method string Method string
@ -254,7 +272,6 @@ func New() (e *Echo) {
// Logger // Logger
e.logger = log.New("echo") e.logger = log.New("echo")
e.logger.SetLevel(log.INFO)
return return
} }
@ -264,23 +281,13 @@ func (e *Echo) Router() *Router {
return e.router return e.router
} }
// SetLogPrefix sets the prefix for the logger. Default value is `echo`. // SetLogger sets the logger instance.
func (e *Echo) SetLogPrefix(prefix string) { func (e *Echo) SetLogger(logger Logger) {
e.logger.SetPrefix(prefix) e.logger = logger
}
// SetLogOutput sets the output destination for the logger. Default value is `os.Stdout`
func (e *Echo) SetLogOutput(w io.Writer) {
e.logger.SetOutput(w)
}
// SetLogLevel sets the log level for the logger. Default value is `log.INFO`.
func (e *Echo) SetLogLevel(l log.Level) {
e.logger.SetLevel(l)
} }
// Logger returns the logger instance. // Logger returns the logger instance.
func (e *Echo) Logger() *log.Logger { func (e *Echo) Logger() Logger {
return e.logger return e.logger
} }

View File

@ -8,6 +8,7 @@ import (
"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"
) )
@ -52,11 +53,13 @@ func TestLogger(t *testing.T) {
func TestLoggerIPAddress(t *testing.T) { func TestLoggerIPAddress(t *testing.T) {
e := echo.New() e := echo.New()
l := log.New("echo")
buf := new(bytes.Buffer)
l.SetOutput(buf)
e.SetLogger(l)
req, _ := http.NewRequest(echo.GET, "/", nil) req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec, e), e) c := echo.NewContext(req, echo.NewResponse(rec, e), e)
buf := new(bytes.Buffer)
e.Logger().SetOutput(buf)
ip := "127.0.0.1" ip := "127.0.0.1"
h := func(c *echo.Context) error { h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")