From a98843b6e58dcda9364099cc43c4d5b888baf00c Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 31 May 2016 18:29:11 -0700 Subject: [PATCH] Logger as interface, fixed #533, fixed #349, fixed #304 Signed-off-by: Vishal Rana --- context.go | 6 +++--- echo.go | 36 ++++++++++++++++-------------------- engine/engine.go | 4 ++-- log/logger.go | 23 +++++++++++++++++++++++ 4 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 log/logger.go diff --git a/context.go b/context.go index ee8a7cf4..6f2625c0 100644 --- a/context.go +++ b/context.go @@ -12,7 +12,7 @@ import ( "time" "github.com/labstack/echo/engine" - "github.com/labstack/gommon/log" + "github.com/labstack/echo/log" "bytes" @@ -162,7 +162,7 @@ type ( SetHandler(HandlerFunc) // Logger returns the `Logger` instance. - Logger() *log.Logger + Logger() log.Logger // Echo returns the `Echo` instance. Echo() *Echo @@ -475,7 +475,7 @@ func (c *context) SetHandler(h HandlerFunc) { c.handler = h } -func (c *context) Logger() *log.Logger { +func (c *context) Logger() log.Logger { return c.echo.logger } diff --git a/echo.go b/echo.go index 3be50e45..80589bd4 100644 --- a/echo.go +++ b/echo.go @@ -49,7 +49,8 @@ import ( "sync" "github.com/labstack/echo/engine" - "github.com/labstack/gommon/log" + "github.com/labstack/echo/log" + glog "github.com/labstack/gommon/log" ) type ( @@ -65,7 +66,7 @@ type ( pool sync.Pool debug bool router *Router - logger *log.Logger + logger log.Logger } // Route contains a handler and information for matching against requests. @@ -225,9 +226,9 @@ func New() (e *Echo) { // Defaults e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler) e.SetBinder(&binder{}) - e.logger = log.New("echo") - e.logger.SetLevel(log.ERROR) - + l := glog.New("echo") + l.SetLevel(glog.ERROR) + e.SetLogger(l) return } @@ -248,9 +249,14 @@ 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) +// Logger returns the logger instance. +func (e *Echo) Logger() log.Logger { + return e.logger +} + +// SetLogger defines a custom logger. +func (e *Echo) SetLogger(l log.Logger) { + e.logger = l } // SetLogOutput sets the output destination for the logger. Default value is `os.Std*` @@ -258,21 +264,11 @@ func (e *Echo) SetLogOutput(w io.Writer) { e.logger.SetOutput(w) } -// SetLogLevel sets the log level for the logger. Default value is `log.ERROR`. +// SetLogLevel sets the log level for the logger. Default value is `glog.ERROR`. func (e *Echo) SetLogLevel(l uint8) { e.logger.SetLevel(l) } -// SetLogger defines a custom logger. -func (e *Echo) SetLogger(l *log.Logger) { - e.logger = l -} - -// Logger returns the logger instance. -func (e *Echo) Logger() *log.Logger { - return e.logger -} - // DefaultHTTPErrorHandler invokes the default HTTP error handler. func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) { code := http.StatusInternalServerError @@ -313,7 +309,7 @@ func (e *Echo) SetRenderer(r Renderer) { // SetDebug enable/disable debug mode. func (e *Echo) SetDebug(on bool) { e.debug = on - e.SetLogLevel(log.DEBUG) + e.SetLogLevel(glog.DEBUG) } // Debug returns debug mode (enabled or disabled). diff --git a/engine/engine.go b/engine/engine.go index e7577d86..5b8e50c9 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -7,7 +7,7 @@ import ( "net" - "github.com/labstack/gommon/log" + "github.com/labstack/echo/log" ) type ( @@ -17,7 +17,7 @@ type ( SetHandler(Handler) // SetLogger sets the logger for the HTTP server. - SetLogger(*log.Logger) + SetLogger(log.Logger) // Start starts the HTTP server. Start() error diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 00000000..40df604b --- /dev/null +++ b/log/logger.go @@ -0,0 +1,23 @@ +package log + +import "io" + +type ( + // Logger defines the logging interface. + Logger interface { + SetOutput(io.Writer) + SetLevel(uint8) + Print(...interface{}) + Printf(string, ...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{}) + } +)