logi_ex v0.1.1 Logi.Filter behaviour

Log Message Filter Behaviour.

A filter decides whether to allow a message be sent to the target channel.

Note

A filter should not raise exceptions when it’s filter/2 is called.

If any exception is raised, the invocation of the log function will be aborted and the exception will be propagated to the caller process.

Examples

iex> require Logi

iex> write = fn (_, format, data) -> :io.format format <> "\n", data end
iex> {:ok, _} = Logi.Channel.install_sink(Logi.BuiltIn.Sink.Fun.new(:foo, write), :info)

iex> filter = fn (context) -> not Map.get(Logi.Context.get_metadata(context), :discard, false) end
iex> logger = Logi.new([filter: Logi.BuiltIn.Filter.Fun.new(filter)])
iex> Logi.save_as_default logger

iex> Logi.info "hello world"
#OUTPUT# hello world

iex> Logi.info "hello world", [], [metadata: %{:discard => true}]
# No output: the log message was discarded by the filter

Summary

Types

A module that implements the Logi.Filter behaviour

An instance of Logi.Filter behaviour implementation module

The value of the second arguemnt of the filter/2 callback function

Functions

Applies filter

Returns true if x is a filter/0 value, false otherwise

Gets the module of filter

Gets the state of filter

Creates a new filter instance

Callbacks

Log messages filtering function

Types

callback_module()
callback_module() :: module

A module that implements the Logi.Filter behaviour.

filter()

An instance of Logi.Filter behaviour implementation module.

state()
state() :: any

The value of the second arguemnt of the filter/2 callback function.

If a filter() does not have an explicit state(), nil will be passed instead.

Functions

apply(context, filter)
apply(Logi.Context.context, filter) ::
  do_allow |
  {do_allow, new_filter} when do_allow: boolean, new_filter: filter

Applies filter.

This function returns do_allow if the state of filter is not changed, {do_allow, new_filter} otherwise.

filter?(x)
filter?(any) :: boolean

Returns true if x is a filter/0 value, false otherwise.

get_module(filter)
get_module(filter) :: callback_module

Gets the module of filter.

get_state(filter)
get_state(filter) :: state

Gets the state of filter.

new(callback_module, state \\ nil)

Creates a new filter instance.

Callbacks

filter(arg0, state)
filter(Logi.Context.context, state) ::
  boolean |
  {boolean, state}

Log messages filtering function.

This function decides whether to allow a message be sent to the target channel. If it returns false (or {false, state}), the message will be dropped.