package chislog import ( "net" "net/http" ) type Filter func(r *http.Request) bool type IPExtractor func(r *http.Request) string type config struct { filters []Filter ipExtractor IPExtractor } type Option func(cfg *config) func newConfig(opts ...Option) *config { cfg := &config{ ipExtractor: defaultIPExtractor, } 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) } } // WithIPExtractor 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. func WithIPExtractor(extractor IPExtractor) Option { return func(c *config) { c.ipExtractor = extractor } } func defaultIPExtractor(r *http.Request) string { ip, _, _ := net.SplitHostPort(r.RemoteAddr) return ip }