Request logging middleware for go-chi using zap.
Go to file
renovate 9e49701dd9
continuous-integration/drone/push Build is passing Details
chore(deps): update golang docker tag to v1.20 (#11)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| golang | docker | minor | `1.19` -> `1.20` |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMTQuMSIsInVwZGF0ZWRJblZlciI6IjM0LjExNC4xIn0=-->

Co-authored-by: Renovate <renovate@dwysokinski.me>
Reviewed-on: #11
Co-authored-by: renovate <renovate@noreply.localhost>
Co-committed-by: renovate <renovate@noreply.localhost>
2023-02-02 04:33:05 +00:00
.drone.yml chore(deps): update golang docker tag to v1.20 (#11) 2023-02-02 04:33:05 +00:00
.gitignore init 2021-08-31 08:03:13 +02:00
LICENSE init 2021-08-31 08:03:13 +02:00
README.md chore: update README.md 2022-12-11 10:15:17 +01:00
config.go refactor (#1) 2022-09-16 14:06:41 +00:00
go.mod chore(deps): update module github.com/go-chi/chi/v5 to v5.0.8 (#10) 2022-12-11 08:02:29 +00:00
go.sum chore(deps): update module github.com/go-chi/chi/v5 to v5.0.8 (#10) 2022-12-11 08:02:29 +00:00
logger.go refactor (#1) 2022-09-16 14:06:41 +00:00
logger_test.go run tests in parallel (#4) 2022-09-16 14:55:21 +00:00
renovate.json refactor (#1) 2022-09-16 14:06:41 +00:00

README.md

chizap Build Status

A request logging middleware for go-chi using zap.

Getting started

Installation

go get gitea.dwysokinski.me/Kichiyaki/chizap

Usage

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