From 4a09f90bf1a9038527493be82be6be024e526530 Mon Sep 17 00:00:00 2001 From: Oleg Lobanov Date: Sun, 31 Jan 2016 11:51:12 +0400 Subject: [PATCH] Replace real object logger with interface. --- echo.go | 39 +++++++++++++++++++++++---------------- middleware/logger_test.go | 7 +++++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/echo.go b/echo.go index 6859bab5..9654a6ff 100644 --- a/echo.go +++ b/echo.go @@ -66,10 +66,28 @@ type ( debug bool hook http.HandlerFunc autoIndex bool - logger *log.Logger + logger Logger 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 struct { Method string @@ -254,7 +272,6 @@ func New() (e *Echo) { // Logger e.logger = log.New("echo") - e.logger.SetLevel(log.INFO) return } @@ -264,23 +281,13 @@ func (e *Echo) Router() *Router { return e.router } -// SetLogPrefix sets the prefix for the logger. Default value is `echo`. -func (e *Echo) SetLogPrefix(prefix string) { - e.logger.SetPrefix(prefix) -} - -// 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) +// SetLogger sets the logger instance. +func (e *Echo) SetLogger(logger Logger) { + e.logger = logger } // Logger returns the logger instance. -func (e *Echo) Logger() *log.Logger { +func (e *Echo) Logger() Logger { return e.logger } diff --git a/middleware/logger_test.go b/middleware/logger_test.go index 298154b4..3ae4970b 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/labstack/echo" + "github.com/labstack/gommon/log" "github.com/stretchr/testify/assert" ) @@ -52,11 +53,13 @@ func TestLogger(t *testing.T) { func TestLoggerIPAddress(t *testing.T) { e := echo.New() + l := log.New("echo") + buf := new(bytes.Buffer) + l.SetOutput(buf) + e.SetLogger(l) req, _ := http.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec, e), e) - buf := new(bytes.Buffer) - e.Logger().SetOutput(buf) ip := "127.0.0.1" h := func(c *echo.Context) error { return c.String(http.StatusOK, "test")