chore: update README.md
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
2432b5bcab
commit
89b13247b1
89
README.md
89
README.md
|
@ -1,13 +1,92 @@
|
||||||
# chizap [![Build Status](https://drone.dwysokinski.me/api/badges/Kichiyaki/chizap/status.svg?ref=refs/heads/master)](https://drone.dwysokinski.me/Kichiyaki/chizap)
|
# chizap [![Build Status](https://drone.dwysokinski.me/api/badges/Kichiyaki/chizap/status.svg?ref=refs/heads/master)](https://drone.dwysokinski.me/Kichiyaki/chizap)
|
||||||
|
|
||||||
Request logging middleware for [go-chi](https://github.com/go-chi/chi) using [zap](https://github.com/uber-go/zap).
|
A request logging middleware for [go-chi](https://github.com/go-chi/chi) using [zap](https://github.com/uber-go/zap).
|
||||||
|
|
||||||
## Installation
|
## Getting started
|
||||||
|
|
||||||
```bash
|
### Installation
|
||||||
|
|
||||||
|
```shell
|
||||||
go get gitea.dwysokinski.me/Kichiyaki/chizap
|
go get gitea.dwysokinski.me/Kichiyaki/chizap
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
### Usage
|
||||||
|
|
||||||
See the [example](./_examples/basic/main.go).
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.dwysokinski.me/Kichiyaki/chizap"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
logger, err := zap.NewDevelopment()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("zap.NewDevelopment", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.ListenAndServe(":8080", newRouter(logger))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRouter(logger *zap.Logger) *chi.Mux {
|
||||||
|
router := chi.NewRouter()
|
||||||
|
|
||||||
|
router.Route("/", func(r chi.Router) {
|
||||||
|
r.Use(chizap.Logger(
|
||||||
|
logger,
|
||||||
|
chizap.WithFilter(func(r *http.Request) bool {
|
||||||
|
return r.URL.Path != "/excluded"
|
||||||
|
}),
|
||||||
|
chizap.WithFilter(func(r *http.Request) bool {
|
||||||
|
return r.URL.Path != "/excluded2"
|
||||||
|
}),
|
||||||
|
chizap.WithAdditionalFieldExtractor(func(r *http.Request) []zap.Field {
|
||||||
|
if !strings.HasPrefix(r.URL.Path, "/delete") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return []zap.Field{
|
||||||
|
zap.String("id", chi.URLParam(r, "id")),
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
})
|
||||||
|
r.Get("/error", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
})
|
||||||
|
r.Get("/excluded", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
r.Delete("/delete/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
router.With(chizap.Logger(logger, chizap.WithTimeFormat(time.RFC1123Z))).
|
||||||
|
Get("/custom-time-format", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Distributed under the MIT License. See ``LICENSE`` for more information.
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
Dawid Wysokiński - [contact@dwysokinski.me](mailto:contact@dwysokinski.me)
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
module gitea.dwysokinski.me/Kichiyaki/chizap/_examples/basic
|
|
||||||
|
|
||||||
go 1.18
|
|
||||||
|
|
||||||
replace gitea.dwysokinski.me/Kichiyaki/chizap => ../..
|
|
||||||
|
|
||||||
require (
|
|
||||||
gitea.dwysokinski.me/Kichiyaki/chizap v0.2.1
|
|
||||||
github.com/go-chi/chi/v5 v5.0.8
|
|
||||||
go.uber.org/zap v1.24.0
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
|
||||||
go.uber.org/multierr v1.7.0 // indirect
|
|
||||||
)
|
|
|
@ -1,25 +0,0 @@
|
||||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
|
|
||||||
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
|
||||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
|
||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
|
||||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
|
||||||
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
|
|
||||||
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
|
||||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
|
||||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
|
@ -1,68 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.dwysokinski.me/Kichiyaki/chizap"
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
logger, err := zap.NewDevelopment()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("zap.NewDevelopment", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
http.ListenAndServe(":8080", newRouter(logger))
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRouter(logger *zap.Logger) *chi.Mux {
|
|
||||||
router := chi.NewRouter()
|
|
||||||
|
|
||||||
router.Route("/", func(r chi.Router) {
|
|
||||||
r.Use(chizap.Logger(
|
|
||||||
logger,
|
|
||||||
chizap.WithFilter(func(r *http.Request) bool {
|
|
||||||
return r.URL.Path != "/excluded"
|
|
||||||
}),
|
|
||||||
chizap.WithFilter(func(r *http.Request) bool {
|
|
||||||
return r.URL.Path != "/excluded2"
|
|
||||||
}),
|
|
||||||
chizap.WithAdditionalFieldExtractor(func(r *http.Request) []zap.Field {
|
|
||||||
if !strings.HasPrefix(r.URL.Path, "/delete") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return []zap.Field{
|
|
||||||
zap.String("id", chi.URLParam(r, "id")),
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
))
|
|
||||||
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
})
|
|
||||||
r.Get("/error", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
})
|
|
||||||
r.Get("/excluded", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
r.Delete("/delete/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
router.With(chizap.Logger(logger, chizap.WithTimeFormat(time.RFC1123Z))).
|
|
||||||
Get("/custom-time-format", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
|
|
||||||
return router
|
|
||||||
}
|
|
Loading…
Reference in New Issue