chizap/config.go

59 lines
1.4 KiB
Go

package chizap
import (
"net/http"
"time"
"go.uber.org/zap"
)
type Filter func(r *http.Request) bool
type AdditionalFieldExtractor func(r *http.Request) []zap.Field
type config struct {
filters []Filter
timeFormat string
additionalFieldExtractors []AdditionalFieldExtractor
}
type Option func(*config)
func newConfig(opts ...Option) *config {
cfg := &config{
timeFormat: time.RFC3339,
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
// 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 WithFilter(f Filter) Option {
return func(c *config) {
c.filters = append(c.filters, f)
}
}
// WithTimeFormat specifies a time format to use for logging a request time.
// time.RFC3339 is used by default.
func WithTimeFormat(f string) Option {
return func(c *config) {
c.timeFormat = f
}
}
// WithAdditionalFieldExtractor takes a function that will be called on every
// request and the returned fields will be added to a log entry.
func WithAdditionalFieldExtractor(extractor AdditionalFieldExtractor) Option {
return func(c *config) {
c.additionalFieldExtractors = append(c.additionalFieldExtractors, extractor)
}
}