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
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
}

View File

@ -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")