Filters

The Deny, Allow, and Shorten builders, their match criteria, and the runtime-mutable filter handler.

A filter selects records and decides what happens to them. Build one from an action, chain match criteria onto it, and pass it to log.WithFilters or a FilterHandler. See Filtering for the task-level walkthrough.

Actions

func Deny() Filter

Starts a filter that drops matching records so no output sees them.

func Allow() Filter

Starts a filter that passes matching records through unchanged.

func Shorten(keys ...string) Filter

Starts a filter that truncates the given attribute keys on matching records. The default length limit is 100. Change it with Limit.

Match criteria

A record must match every criterion set on a filter for its action to apply. A criterion left unset is ignored.

func (f Filter) Message(msg string) Filter

Matches a record whose message equals msg exactly.

func (f Filter) Attr(key string, val string) Filter

Matches when the record's attribute key equals val. A val ending in * matches by prefix. The record's message is available under the synthetic "msg" key.

func (f Filter) Below(level slog.Level) Filter

Matches records strictly below level, so Below(slog.LevelInfo) matches DEBUG and TRACE. Paired with Deny it acts as a level floor.

func (f Filter) Limit(n int) Filter

Sets the Shorten length limit.

FilterHandler

func NewFilterHandler(handler slog.Handler, filters ...Filter) *FilterHandler

Wraps a handler with an ordered list of filters, applied to each record before it reaches the wrapped handler. Filters run in order, and the first Deny match drops the record. log.WithFilters builds one of these for you.

The filter list can change while logging.

func (f *FilterHandler) AddFilter(filter Filter)
func (f *FilterHandler) SetFilters(filters []Filter)

AddFilter appends one filter and SetFilters replaces the whole list. Both are safe to call concurrently with active logging.