Copyright © 2014-2016 Takeru Ohta <phjgt308@gmail.com>
This module defines the logi_filter behaviour.
Required callback functions: filter/2.
Log Message Filter Behaviour
A filter decides whether to allow a message be sent to the target channel.
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.
> 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
callback_module() = module()
A module that implements the logi_filter
behaviour.
abstract datatype: filter()
An instance of logi_filter
behaviour implementation module.
state() = term()
The value of the second arguemnt of the filter/2
callback function.
filter()
does not have an explicit state()
, undefined
will be passed instead.
apply/2 | Applies Filter |
get_module/1 | Gets the module of Filter |
get_state/1 | Gets the state of Filter |
is_filter/1 | Returns true if X is a filter, false otherwise. |
new/1 | Equivalent to new(Module, undefined). |
new/2 | Creates a new filter instance. |
apply(Context::logi_context:context(), Filter::filter()) -> DoAllow | {DoAllow, NewFilter}
Applies Filter
DoAllow
if the state of Filter
is not changed, {DoAllow, NewFilter}
otherwise.
get_module(Filter::filter()) -> callback_module()
Gets the module of Filter
Gets the state of Filter
is_filter(X::filter() | term()) -> boolean()
Returns true
if X
is a filter, false
otherwise
new(Module::callback_module()) -> filter()
Equivalent to new(Module, undefined).
new(Module::callback_module(), State::state()) -> filter()
Creates a new filter instance
Generated by EDoc