Skip to content

Commit

Permalink
Merge pull request #3 from eko/added-logrus-support
Browse files Browse the repository at this point in the history
Added Logrus library as a logger
  • Loading branch information
eko committed Oct 4, 2018
2 parents 9dff587 + ad81a8d commit d8eb867
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
36 changes: 36 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -9,3 +9,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.1.0"
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -17,6 +17,7 @@ Prior to Go 1.11:
$ git clone git@github.com:eko/gofast.git
$ go get -u github.com/flosch/pongo2
$ go get -u golang.org/x/net/http2
$ go get -u golang.org/sirupsen/logrus
```

Prefer using `dep`?
Expand Down Expand Up @@ -191,3 +192,15 @@ app.Get("retrieve-data", "/retrieve$", func(context gofast.Context) {
fmt.Fprint(response, "{result: 200}")
})
```

Use the logger
--------------

The Logrus logger instance can be used to write information in your application stdout if you need it. You can retrieve the logger instance from the context as follows:

```go
app.Get("test-logger", "/test-logger$", func(context gofast.Context) {
logger := context.GetLogger()
logger.Info("Roger, this is my log information!")
})
```
13 changes: 13 additions & 0 deletions context.go
Expand Up @@ -6,9 +6,12 @@ package gofast

import (
"net/http"

"github.com/sirupsen/logrus"
)

type Context struct {
logger *logrus.Logger
request *Request
response *Response
route *Route
Expand All @@ -19,6 +22,16 @@ func NewContext() Context {
return Context{}
}

// Sets Logrus logger instance
func (c *Context) SetLogger(logger *logrus.Logger) {
c.logger = logger
}

// Returns a Logrus logger instance
func (c *Context) GetLogger() *logrus.Logger {
return c.logger
}

// Sets a HTTP request instance
func (c *Context) SetRequest(req *http.Request) {
request := NewRequest(req)
Expand Down
19 changes: 14 additions & 5 deletions gofast.go
Expand Up @@ -6,12 +6,12 @@ package gofast

import (
"fmt"
"log"
"net/http"
"os"
"sort"
"time"

"github.com/sirupsen/logrus"
"golang.org/x/net/http2"
)

Expand All @@ -21,20 +21,22 @@ const (
)

type Gofast struct {
*logrus.Logger
*Router
*Templating
*Middleware
}

// Bootstraps a new instance
func Bootstrap() *Gofast {
log.Printf("gofast v%s", VERSION)
logrus.WithFields(logrus.Fields{"version": VERSION}).Info("gofast is running")

logger := logrus.New()
router := NewRouter()
templating := NewTemplating()
middleware := NewMiddleware()

return &Gofast{&router, &templating, &middleware}
return &Gofast{logger, &router, &templating, &middleware}
}

// Prepares a HTTP server
Expand Down Expand Up @@ -70,7 +72,7 @@ func (g *Gofast) ListenHttp2(certificate string, key string) {
server := &http.Server{Addr: port, Handler: nil}

http2.ConfigureServer(server, nil)
log.Fatal(server.ListenAndServeTLS(certificate, key))
logrus.Fatal(server.ListenAndServeTLS(certificate, key))
}

// Serves HTTP request by matching the correct route
Expand All @@ -96,6 +98,7 @@ func (g *Gofast) HandleRoute(res http.ResponseWriter, req *http.Request, route R
startTime := time.Now()

context := NewContext()
context.SetLogger(g.Logger)
context.SetRoute(&route)
context.SetRequest(req)
context.SetResponse(res)
Expand All @@ -105,5 +108,11 @@ func (g *Gofast) HandleRoute(res http.ResponseWriter, req *http.Request, route R

stopTime := time.Now()

log.Printf("[%s] %v | route: '%s' | url: %q (time: %v)\n", req.Method, context.GetResponse().GetStatusCode(), route.name, req.URL.String(), stopTime.Sub(startTime))
logrus.WithFields(logrus.Fields{
"name": route.name,
"method": req.Method,
"code": context.GetResponse().GetStatusCode(),
"url": req.URL.String(),
"duration": stopTime.Sub(startTime),
}).Info("Route matched")
}
11 changes: 6 additions & 5 deletions templating.go
Expand Up @@ -6,9 +6,10 @@ package gofast

import (
"fmt"
"github.com/flosch/pongo2"
"log"
"os"

"github.com/flosch/pongo2"
"github.com/sirupsen/logrus"
)

type Templating struct {
Expand All @@ -25,7 +26,7 @@ func NewTemplating() Templating {
func (t *Templating) SetViewsDirectory(name string) {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
log.Printf("Directory '%s' does not exists", name)
logrus.Warn(fmt.Sprintf("Directory '%s' does not exists", name))
os.Exit(1)
}
}
Expand All @@ -42,7 +43,7 @@ func (t *Templating) GetViewsDirectory() string {
func (t *Templating) SetAssetsDirectory(name string) {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
log.Printf("Directory '%s' does not exists", name)
logrus.Warn(fmt.Sprintf("Directory '%s' does not exists", name))
os.Exit(1)
}
}
Expand All @@ -61,7 +62,7 @@ func (t *Templating) Render(context Context, name string) {

if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
log.Printf("View '%s' does not exists", filename)
logrus.Warn(fmt.Sprintf("View '%s' does not exists", filename))
os.Exit(1)
}
}
Expand Down

0 comments on commit d8eb867

Please sign in to comment.