1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Logger as not an interface

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-06 09:52:32 -08:00
parent a9c88cad63
commit e1d789ecbb
31 changed files with 152 additions and 86 deletions

6
.gitignore vendored
View File

@ -1,9 +1,6 @@
# Website
website/public
website/make
website/Makefile
website/marathon*
.gitmodules
# Node.js
node_modules
@ -11,3 +8,6 @@ node_modules
# IntelliJ
.idea
*.iml
# Glide
vendor

View File

@ -11,7 +11,7 @@ import (
"time"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"net/url"
@ -51,7 +51,7 @@ type (
Redirect(int, string) error
Error(err error)
Handle(Context) error
Logger() logger.Logger
Logger() *log.Logger
Echo() *Echo
Object() *context
}
@ -312,7 +312,7 @@ func (c *context) Echo() *Echo {
}
// Logger returns the `Logger` instance.
func (c *context) Logger() logger.Logger {
func (c *context) Logger() *log.Logger {
return c.echo.logger
}

25
echo.go
View File

@ -15,7 +15,6 @@ import (
"encoding/xml"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
@ -32,7 +31,7 @@ type (
pool sync.Pool
debug bool
router *Router
logger logger.Logger
logger *log.Logger
}
Route struct {
@ -204,6 +203,7 @@ func New() (e *Echo) {
// Logger
e.logger = log.New("echo")
e.logger.SetLevel(log.FATAL)
return
}
@ -221,13 +221,23 @@ func (e *Echo) Router() *Router {
return e.router
}
// SetLogger sets the logger instance.
func (e *Echo) SetLogger(l logger.Logger) {
e.logger = l
// 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.Std*`
func (e *Echo) SetLogOutput(w io.Writer) {
e.logger.SetOutput(w)
}
// SetLogLevel sets the log level for the logger. Default value is `log.FATAL`.
func (e *Echo) SetLogLevel(l log.Level) {
e.logger.SetLevel(l)
}
// Logger returns the logger instance.
func (e *Echo) Logger() logger.Logger {
func (e *Echo) Logger() *log.Logger {
return e.logger
}
@ -245,7 +255,7 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
if !c.Response().Committed() {
c.String(code, msg)
}
e.logger.Error(err)
e.logger.Debug(err)
}
// SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
@ -266,6 +276,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)
}
// Debug returns debug mode (enabled or disabled).

View File

@ -4,7 +4,7 @@ import (
"io"
"time"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
type (
@ -12,7 +12,7 @@ type (
Engine interface {
SetHandler(HandlerFunc)
SetLogger(logger.Logger)
SetLogger(*log.Logger)
Start()
}

View File

@ -7,7 +7,6 @@ import (
"net/http"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
@ -20,7 +19,7 @@ type (
size int64
committed bool
writer io.Writer
logger logger.Logger
logger *log.Logger
}
)

View File

@ -6,7 +6,6 @@ import (
"sync"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
@ -16,7 +15,7 @@ type (
config *engine.Config
handler engine.HandlerFunc
pool *Pool
logger logger.Logger
logger *log.Logger
}
Pool struct {
@ -73,7 +72,7 @@ func NewConfig(c *engine.Config) (s *Server) {
},
},
handler: func(req engine.Request, res engine.Response) {
s.logger.Warn("handler not set")
s.logger.Fatal("handler not set")
},
logger: log.New("echo"),
}
@ -84,7 +83,7 @@ func (s *Server) SetHandler(h engine.HandlerFunc) {
s.handler = h
}
func (s *Server) SetLogger(l logger.Logger) {
func (s *Server) SetLogger(l *log.Logger) {
s.logger = l
}

View File

@ -5,7 +5,7 @@ import (
"net/http"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
type (
@ -16,11 +16,11 @@ type (
size int64
committed bool
writer io.Writer
logger logger.Logger
logger *log.Logger
}
)
func NewResponse(w http.ResponseWriter, l logger.Logger) *Response {
func NewResponse(w http.ResponseWriter, l *log.Logger) *Response {
return &Response{
response: w,
header: &Header{w.Header()},

View File

@ -5,7 +5,6 @@ import (
"sync"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
@ -15,7 +14,7 @@ type (
config *engine.Config
handler engine.HandlerFunc
pool *Pool
logger logger.Logger
logger *log.Logger
}
Pool struct {
@ -78,7 +77,7 @@ func (s *Server) SetHandler(h engine.HandlerFunc) {
s.handler = h
}
func (s *Server) SetLogger(l logger.Logger) {
func (s *Server) SetLogger(l *log.Logger) {
s.logger = l
}

16
glide.lock generated
View File

@ -1,18 +1,18 @@
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
updated: 2016-02-22T21:25:23.014357426-08:00
updated: 2016-03-06T08:57:09.146198268-08:00
imports:
- name: github.com/klauspost/compress
version: f9625351863b5e94c1da72862187b8fe9a91af90
version: 2d3d403f37d2e70b722590bc286076a17422e1f2
subpackages:
- flate
- gzip
- zlib
- name: github.com/klauspost/cpuid
version: 2c698c6aef5976c7860074cc7040e8af7866aa21
version: 09cded8978dc9e80714c4d85b0322337b0a1e5e0
- name: github.com/klauspost/crc32
version: 19b0b332c9e4516a6370a0456e6182c3b5036720
- name: github.com/labstack/gommon
version: bfff5bf04688a4048a5cb4dd3b3f0697caaad19c
version: c7a42f4800da9d39225ce15411f48288d622e517
subpackages:
- color
- log
@ -21,10 +21,14 @@ imports:
- name: github.com/mattn/go-isatty
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
- name: github.com/valyala/fasthttp
version: 115b98672ee1c023598362dc74e9b04adaa7dede
version: 57df0ba8a413b7f236b6f64aa6b8b5419ffe828f
- name: golang.org/x/net
version: 0899459b4d84739aa2600ba248120318d78f8997
version: 08f168e593b5aab61849054b77981de812666697
subpackages:
- context
- websocket
- name: golang.org/x/sys
version: 7a56174f0086b32866ebd746a794417edbc678a1
subpackages:
- unix
devImports: []

View File

@ -1,21 +0,0 @@
package logger
type (
// 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{})
}
)

View File

@ -52,7 +52,7 @@ func Logger(options ...*LoggerOptions) echo.MiddlewareFunc {
code = color.Cyan(n)
}
logger.Infof("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
logger.Printf("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
return nil
})
}

View File

@ -41,13 +41,7 @@ SetLogOutput sets the output destination for the logger. Default value is `os.St
`echo#SetLogLevel(l log.Level)`
SetLogLevel sets the log level for the logger. Default value is `log.INFO`.
### HTTP2
`echo#HTTP(on bool)`
Enable/disable HTTP2 support.
SetLogLevel sets the log level for the logger. Default value is `log.FATAL`.
### Auto index

View File

@ -0,0 +1,63 @@
---
title: Migrating
menu:
side:
parent: guide
weight: 2
---
### Migrating from v1
#### What got changed?
- Echo now uses `Engine` interface to abstract `HTTP` server implementation, allowing
us to use HTTP servers beyond Go standard library, supports standard HTTP server and [FastHTTP](https://github.com/valyala/fasthttp).
- Context, Request and Response as an interface, enabling adding your own functions and easy testing. [More...](https://github.com/labstack/echo/issues/146)
- Moved API's for serving static files into middleware.
- `Echo#Index`
- `Echo#Favicon`
- `Echo#Static`
- `Echo#ServeDir`
- `Echo#ServeFile`
- Dropped auto wrapping of handler and middleware to enforce compile time check.
- Handler only accepts `Echo#Handler` interface.
- Middleware only accepts `Echo#Middleware` interface.
- `Echo#HandlerFunc` adapter to use of ordinary functions as handlers.
- `Echo#MiddlewareFunc` adapter to use of ordinary functions as middleware.
- Middleware is run before hitting the router, which doesn't require `Echo#Hook` API as
it can be achieved via middleware.
- Ability to define middleware at route level.
#### How?
##### v1 Handler
```go
func welcome(c *echo.Context) error {
return c.String(http.StatusOK, "Welcome!\n")
}
```
##### v2 Handler
```go
func welcome(echo.HandlerFunc(c echo.Context) error {
return c.String(http.StatusOK, "Welcome!\n")
})
```
##### v1 Middleware
```go
func welcome(c *echo.Context) error {
return c.String(http.StatusOK, "Welcome!\n")
}
```
v2
```go
func welcome(echo.HandlerFunc(c echo.Context) error {
return c.String(http.StatusOK, "Welcome!\n")
})
```

View File

@ -8,12 +8,12 @@ menu:
### Server
`server.go`
`main.go`
{{< embed "crud/server.go" >}}
{{< embed "crud/main.go" >}}
### Maintainers
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/crud)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/crud)

View File

@ -17,4 +17,4 @@ menu:
- [caarlos0](https://github.com/caarlos0)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/rice)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/rice)

View File

@ -50,4 +50,4 @@ if _, err = io.Copy(dst, file); err != nil {
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/file-upload)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/file-upload)

View File

@ -132,4 +132,4 @@ but is outside the scope of this recipe.
- [CaptainCodeman](https://github.com/CaptainCodeman)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/google-app-engine)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/google-app-engine)

View File

@ -24,6 +24,6 @@ menu:
### Source Code
[graceful](https://github.com/vishr/recipes/blob/master/echo/recipes/graceful-shutdown/graceful)
[graceful](https://github.com/vishr/echo-recipes/blob/master/v2/graceful-shutdown/graceful)
[grace](https://github.com/vishr/recipes/blob/master/echo/recipes/graceful-shutdown/grace)
[grace](https://github.com/vishr/echo-recipes/blob/master/v2/graceful-shutdown/grace)

View File

@ -8,12 +8,12 @@ menu:
### Server
`server.go`
`main.go`
{{< embed "hello-world/server.go" >}}
{{< embed "hello-world/main.go" >}}
### Maintainers
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/hello-world)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/hello-world)

View File

@ -10,9 +10,9 @@ JSONP is a method that allows cross-domain server calls. You can read more about
### Server
`server.go`
`main.go`
{{< embed "jsonp/server.go" >}}
{{< embed "jsonp/main.go" >}}
### Client
@ -24,4 +24,4 @@ JSONP is a method that allows cross-domain server calls. You can read more about
- [willf](https://github.com/willf)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/jsonp)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/jsonp)

View File

@ -55,4 +55,4 @@ $ curl localhost:1323/restricted -H "Authorization: Bearer <token>" => Access g
- [axdg](https://github.com/axdg)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/jwt-authentication)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/jwt-authentication)

View File

@ -9,12 +9,12 @@ menu:
### Server
`server.go`
`main.go`
{{< embed "middleware/server.go" >}}
{{< embed "middleware/main.go" >}}
### Maintainers
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/middleware)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/middleware)

View File

@ -25,4 +25,4 @@ menu:
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/streaming-file-upload)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/streaming-file-upload)

View File

@ -35,4 +35,4 @@ $ curl localhost:1323
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/streaming-response)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/streaming-response)

View File

@ -15,4 +15,4 @@ menu:
- [axdg](https://github.com/axdg)
- [vishr](https://github.com/axdg)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/subdomains)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/subdomains)

View File

@ -22,4 +22,4 @@ menu:
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/website)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/website)

View File

@ -44,4 +44,4 @@ Hello, Server!
- [vishr](https://github.com/vishr)
### [Source Code](https://github.com/vishr/recipes/blob/master/echo/recipes/websocket)
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/websocket)

View File

@ -1,2 +1,2 @@
<pre data-src="https://raw.githubusercontent.com/vishr/recipes/master/echo/recipes/{{ .Get 0 }}">
<pre data-src="https://raw.githubusercontent.com/vishr/echo-recipes/master/v2/{{ .Get 0 }}">
</pre>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="46px" height="46px" viewBox="0 0 46 46" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
<g sketch:type="MSArtboardGroup" transform="translate(-639.000000, -1341.000000)">
<g sketch:type="MSLayerGroup" transform="translate(133.000000, 1155.000000)">
<g id="Aim" transform="translate(507.000000, 187.000000)" sketch:type="MSShapeGroup">
<g id="Main">
<circle id="Oval-88" stroke="#2196f3" stroke-width="2" cx="22" cy="22" r="22"></circle>
<circle id="Oval-89" stroke="#2196f3" stroke-width="2" cx="22" cy="22" r="15.3043478"></circle>
<circle id="Oval-90" stroke="#2196f3" stroke-width="2" cx="22" cy="22" r="8.60869565"></circle>
<circle id="Oval-91" fill="#2196f3" cx="22" cy="22" r="1.91304348"></circle>
</g>
</g>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -0,0 +1 @@
<svg width="127" height="19" viewBox="0 0 127 19" xmlns="http://www.w3.org/2000/svg"><title>LabStack</title><path d="M16.242 19H2.344c-.22 0-.422-.04-.61-.117a1.49 1.49 0 0 1-.486-.322 1.49 1.49 0 0 1-.322-.486 1.568 1.568 0 0 1-.117-.61V2.195h3.045v13.76h12.387V19zm14.977-4.195c0 .422-.075.877-.224 1.365a4.08 4.08 0 0 1-.72 1.36 4.018 4.018 0 0 1-1.3 1.048c-.537.28-1.187.422-1.953.422h-5.496a4.71 4.71 0 0 1-1.365-.223 4.08 4.08 0 0 1-1.36-.72 4.018 4.018 0 0 1-1.048-1.3c-.28-.536-.422-1.187-.422-1.952 0-.422.074-.88.223-1.37a4.07 4.07 0 0 1 .72-1.367 4.018 4.018 0 0 1 1.3-1.048c.536-.282 1.187-.422 1.952-.422h5.496v2.906h-5.496c-.414 0-.734.127-.96.38-.227.255-.34.57-.34.944 0 .4.13.71.392.932.26.222.57.334.93.334h5.473c.415 0 .735-.125.96-.375.228-.25.34-.564.34-.94v-4.253c0-.398-.122-.714-.368-.95-.246-.233-.557-.35-.932-.35h-6.69V6.332h6.69c.422 0 .877.074 1.366.223.487.148.94.388 1.358.72.418.332.768.766 1.05 1.3.28.536.42 1.187.42 1.952v4.278zm16.57-.082a4.696 4.696 0 0 1-.41 1.87c-.142.315-.323.618-.546.907-.223.29-.492.545-.81.768-.315.222-.68.4-1.094.533-.414.134-.883.2-1.407.2h-5.496a4.696 4.696 0 0 1-1.87-.41 4.445 4.445 0 0 1-.907-.545 3.697 3.697 0 0 1-.768-.81 4.01 4.01 0 0 1-.533-1.1c-.134-.418-.2-.89-.2-1.412V.977h3.047v13.746c0 .375.117.673.35.896.236.222.53.333.88.333h5.496c.383 0 .682-.113.897-.34.215-.226.322-.523.322-.89v-4.125c0-.383-.113-.682-.34-.897-.226-.214-.52-.32-.88-.32h-5.495V6.33h5.496a4.696 4.696 0 0 1 1.87.41c.316.14.62.322.908.545.29.223.544.492.763.81.218.315.394.68.527 1.094.133.415.2.884.2 1.408v4.125zm19.09-.668a5.36 5.36 0 0 1-.23 1.623c-.152.48-.353.904-.603 1.27a4.03 4.03 0 0 1-.88.94 5.39 5.39 0 0 1-1.042.632c-.36.164-.725.285-1.096.363-.372.078-.72.117-1.05.117H50.227v-3.047H61.98c.586 0 1.04-.172 1.366-.515.324-.344.486-.805.486-1.383 0-.282-.043-.54-.13-.774a1.783 1.783 0 0 0-.368-.608 1.626 1.626 0 0 0-.586-.4 2.02 2.02 0 0 0-.768-.14h-7.007a5.43 5.43 0 0 1-1.594-.263 4.78 4.78 0 0 1-1.59-.85c-.487-.39-.894-.903-1.218-1.536-.324-.632-.486-1.406-.486-2.32 0-.914.162-1.685.486-2.314.324-.63.73-1.14 1.22-1.536a4.727 4.727 0 0 1 1.587-.855 5.43 5.43 0 0 1 1.593-.265h10.37v3.047h-10.37c-.578 0-1.03.176-1.354.528-.325.35-.487.816-.487 1.394 0 .586.162 1.05.486 1.39.323.34.775.508 1.353.508h7.031a5.38 5.38 0 0 1 2.14.516c.362.172.708.39 1.036.65.328.262.62.577.873.944.254.367.455.79.603 1.265.15.477.223 1.016.223 1.618zM80.05 9.38h-5.32V19h-3.082V9.38h-3.96V6.33h3.96V2.195h3.082v4.137h5.32V9.38zm14.356 5.425c0 .422-.074.877-.222 1.365a4.08 4.08 0 0 1-.72 1.36 4.018 4.018 0 0 1-1.302 1.048c-.535.28-1.185.422-1.95.422h-5.497a4.71 4.71 0 0 1-1.365-.223 4.08 4.08 0 0 1-1.36-.72 4.018 4.018 0 0 1-1.05-1.3c-.28-.536-.42-1.187-.42-1.952 0-.422.074-.88.222-1.37a4.07 4.07 0 0 1 .72-1.367 4.018 4.018 0 0 1 1.302-1.048c.535-.282 1.185-.422 1.95-.422h5.497v2.906h-5.495c-.414 0-.735.127-.96.38-.228.255-.34.57-.34.944 0 .4.13.71.392.932.26.222.572.334.93.334h5.474c.415 0 .735-.125.962-.375.226-.25.34-.564.34-.94v-4.253c0-.398-.123-.714-.37-.95-.246-.233-.556-.35-.93-.35H83.52V6.332h6.69c.423 0 .878.074 1.366.223.488.148.942.388 1.36.72.418.332.767.766 1.048 1.3.282.536.422 1.187.422 1.952v4.278zM109.276 19h-8.26c-.422 0-.883-.074-1.383-.223a4.067 4.067 0 0 1-1.39-.732c-.425-.34-.78-.78-1.065-1.324-.285-.542-.428-1.208-.428-1.997v-4.125a4.696 4.696 0 0 1 .41-1.87c.14-.316.322-.62.545-.908.223-.29.492-.543.81-.76.315-.22.68-.396 1.094-.53.413-.132.882-.198 1.406-.198h8.26V9.38h-8.26c-.4 0-.702.1-.91.304-.206.203-.31.515-.31.937v4.103c0 .398.106.703.317.914.21.21.52.316.926.316h8.237V19zm17.404 0h-4.395l-6.14-5.93a1.415 1.415 0 0 1-.48-1.172 1.59 1.59 0 0 1 .17-.615c.097-.19.23-.353.403-.486l5.567-4.488h4.875l-7.172 5.776L126.68 19zm-11.79 0h-3.046V.977h3.047V19z" fill="#FFF" fill-rule="evenodd"/></svg>

After

Width:  |  Height:  |  Size: 3.7 KiB