Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dre1080 committed Jun 26, 2020
0 parents commit e15d5f9
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# fiberlog

[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/dre1080/fiberlog)

HTTP request/response logger for Fiber using [zerolog](https://github.com/rs/zerolog).

### Install

```sh
go get -u github.com/gofiber/fiber
go get -u github.com/dre1080/fiberlog
```

### Usage

```go
package main

import (
"github.com/gofiber/fiber"
"github.com/dre1080/fiberlog"
)

func main() {
app := fiber.New()

// Default
app.Use(fiberlog.New())

// Custom Config
app.Use(fiberlog.New(fiberlog.Config{
Logger: &zerolog.New(os.Stdout),
Next: func(ctx *fiber.Ctx) bool {
return ctx.Path() != "/private"
},
}))

app.Listen(3000)
}
```

### Example

Run app server:

```sh
$ go run example/main.go
```

Test request:

```sh
$ curl http://localhost:3000/ok
$ curl http://localhost:3000/warn
$ curl http://localhost:3000/err
```

![screen](./example/screen.png)
27 changes: 27 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/dre1080/fiberlog"
"github.com/gofiber/fiber"
)

func main() {
app := fiber.New()

// Default
app.Use(fiberlog.New())

app.Get("/ok", func(c *fiber.Ctx) {
c.SendString("ok")
})

app.Get("/warn", func(c *fiber.Ctx) {
c.Next(fiber.ErrUnprocessableEntity)
})

app.Get("/err", func(c *fiber.Ctx) {
c.Next(fiber.ErrInternalServerError)
})

app.Listen(3000)
}
Binary file added example/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions fiberlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fiberlog

import (
"net/http"
"os"
"time"

"github.com/gofiber/fiber"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// Config defines the config for logger middleware.
type Config struct {
// Next defines a function to skip this middleware.
Next func(ctx *fiber.Ctx) bool

// Logger is a *zerolog.Logger that writes the logs.
//
// Default: log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
Logger *zerolog.Logger
}

// New is a zerolog middleware that allows you to pass a Config.
//
// app := fiber.New()
//
// // Without config
// app.Use(New())
//
// // With config
// app.Use(New(Config{Logger: &zerolog.New(os.Stdout)}))
func New(config ...Config) fiber.Handler {
var conf Config
if len(config) > 0 {
conf = config[0]
}

var sublog zerolog.Logger
if conf.Logger == nil {
sublog = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
} else {
sublog = *conf.Logger
}

return func(c *fiber.Ctx) {
// Don't execute the middleware if Next returns true
if conf.Next != nil && conf.Next(c) {
c.Next()
return
}

start := time.Now()

// handle request
c.Next()

msg := "Request"
if err := c.Error(); err != nil {
msg = err.Error()
}

code := c.Fasthttp.Response.StatusCode()

dumplogger := sublog.With().
Int("status", code).
Str("method", c.Method()).
Str("path", c.Path()).
Str("ip", c.IP()).
Str("latency", time.Since(start).String()).
Str("user-agent", c.Get(fiber.HeaderUserAgent)).
Logger()

switch {
case code >= fiber.StatusBadRequest && code < fiber.StatusInternalServerError:
dumplogger.Warn().Msg(msg)
case code >= http.StatusInternalServerError:
dumplogger.Error().Msg(msg)
default:
dumplogger.Info().Msg(msg)
}
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/dre1080/fiberlog

go 1.14

require (
github.com/gofiber/fiber v1.12.0
github.com/rs/zerolog v1.19.0
)
29 changes: 29 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/gofiber/fiber v1.12.0 h1:0utSHOWL2FIz/OAHa/VQACJek24FDeBN8FTRV8izUWQ=
github.com/gofiber/fiber v1.12.0/go.mod h1:yFeAjCJH91bgsVXSRuJ8phno0O6/PcGHvjndFw91Vxs=
github.com/gofiber/utils v0.0.6 h1:lWuJfXQ06aFPVA2/y7lUw9ahS4TuXhbwXnxKl1D/fuY=
github.com/gofiber/utils v0.0.6/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
github.com/valyala/fasthttp v1.14.0/go.mod h1:ol1PCaL0dX20wC0htZ7sYCsvCYmrouYra0zHzaclZhE=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit e15d5f9

Please sign in to comment.