Module logi_filter

Log Message Filter Behaviour.

Copyright © 2014-2016 Takeru Ohta <phjgt308@gmail.com>

This module defines the logi_filter behaviour.
Required callback functions: filter/2.

Description

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.

EXAMPLE

  > error_logger:tty(false). % Suppresses annoying warning outputs for brevity
 
  > Context0 = logi_context:new(sample_log, info).
  > FilterFun = fun (C) -> not maps:get(discard, logi_context:get_metadata(C), false) end.
  > Filter = logi_builtin_filter_fun:new(FilterFun).
  > logi_filter:apply(Context0, Filter).
  true
  > Context1 = logi_context:from_map(maps:put(metadata, #{discard => true}, logi_context:to_map(Context0))).
  > logi_filter:apply(Context1, Filter).
  false
  
A more realistic example:
  > WriteFun = fun (_, Format, Data) -> io:format(Format ++ "\n", Data) end.
  > {ok, _} = logi_channel:install_sink(logi_builtin_sink_fun:new(foo, WriteFun), info).
 
  > FilterFun = fun (C) -> not maps:get(discard, logi_context:get_metadata(C), false) end.
  > Logger = logi:new([{filter, logi_builtin_filter_fun:new(FilterFun)}]).
  > logi:save_as_default(Logger).
 
  > logi:info("hello world").
  hello world
 
  > logi:info("hello world", [], [{metadata, #{discard => true}}]).
  % No output: the log message was discarded by the filter
  

Data Types

callback_module()

callback_module() = module()

A module that implements the logi_filter behaviour.

filter()

abstract datatype: filter()

An instance of logi_filter behaviour implementation module.

state()

state() = term()

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

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

Function Index

apply/2Applies Filter
get_module/1Gets the module of Filter
get_state/1Gets the state of Filter
is_filter/1Returns true if X is a filter, false otherwise.
new/1Equivalent to new(Module, undefined).
new/2Creates a new filter instance.

Function Details

apply/2

apply(Context::logi_context:context(), Filter::filter()) -> DoAllow | {DoAllow, NewFilter}

Applies Filter

This function returns DoAllow if the state of Filter is not changed, {DoAllow, NewFilter} otherwise.

get_module/1

get_module(Filter::filter()) -> callback_module()

Gets the module of Filter

get_state/1

get_state(Filter::filter()) -> state()

Gets the state of Filter

is_filter/1

is_filter(X::filter() | term()) -> boolean()

Returns true if X is a filter, false otherwise

new/1

new(Module::callback_module()) -> filter()

Equivalent to new(Module, undefined).

new/2

new(Module::callback_module(), State::state()) -> filter()

Creates a new filter instance


Generated by EDoc