chizap

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 7 Imported by: 0

README

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"

	"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)
		})
	})

	return router
}

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Dawid Wysokiński - contact@dwysokinski.me

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger(logger *zap.Logger, opts ...Option) func(next http.Handler) http.Handler

Logger returns a go-chi middleware that logs requests using go.uber.org/zap.

Types

type AdditionalFieldExtractor

type AdditionalFieldExtractor func(r *http.Request) []zap.Field

type Filter

type Filter func(r *http.Request) bool

type Option

type Option func(*config)

func WithAdditionalFieldExtractor

func WithAdditionalFieldExtractor(extractor AdditionalFieldExtractor) Option

WithAdditionalFieldExtractor takes a function that will be called on every request and the returned fields will be added to the log entry.

func WithFilter

func WithFilter(f Filter) Option

WithFilter adds a filter to the list of filters used by the middleware. If any filter indicates to exclude a request then the request will not be logged. All filters must allow a request to be logged. If no filters are provided, then all requests are logged.

func WithIPFn added in v0.4.0

func WithIPFn(fn func(r *http.Request) string) Option

WithIPFn takes a function that will be called on every request and the returned ip will be added to the log entry. http.Request RemoteAddr is logged by default.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL