| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/Filter.R |
| 3 |
\name{EventFilter} |
| 4 |
\alias{EventFilter} |
| 5 |
\alias{Filter} |
| 6 |
\alias{.obj} |
| 7 |
\title{Event Filters} |
| 8 |
\usage{ |
| 9 |
.obj() |
| 10 |
} |
| 11 |
\description{ |
| 12 |
EventFilters specify arbitrarily complex logic for whether or |
| 13 |
not a LogEvent should be processed by a \link{Logger} or \link{Appender}. They are |
| 14 |
attached to Loggers/Appenders via their \verb{$set_filter()} or \verb{$add_filter()} |
| 15 |
methods. If any EventFilter evaluates to \code{FALSE} for a given event, that |
| 16 |
event is ignored - similarly to when it does not pass the objects' |
| 17 |
threshold. |
| 18 |
|
| 19 |
Usually you do not need to instantiate a formal \code{EventFilter} object as you |
| 20 |
can just use any \code{function} that has the single argument \code{event} instead. |
| 21 |
If you need to implement more complex filter logic - for example a filter |
| 22 |
that is dependent on a dataset - it might be desirable to subclass |
| 23 |
EventFilter, as \link[R6:R6Class]{R6::R6} objects can store data and functions together. |
| 24 |
|
| 25 |
\code{.obj()} is a special function that can only be used within the |
| 26 |
\verb{$filter()} methods of \link[=EventFilter]{EventFilters}. It returns the \link{Logger} |
| 27 |
or \link{Appender} that the EventFilter is attached to. |
| 28 |
} |
| 29 |
\section{Modifying LogEvents with EventFilters}{ |
| 30 |
|
| 31 |
|
| 32 |
Since LogEvents are R6 objects with reference semantics, EventFilters can be |
| 33 |
abused to modify events before passing them on. lgr comes with a few |
| 34 |
preset filters that use this property: \link{FilterInject} (similar to |
| 35 |
\code{\link[=with_log_level]{with_log_level()}}) and \link{FilterForceLevel} (similar to \code{\link[=with_log_value]{with_log_value()}}). |
| 36 |
|
| 37 |
\strong{NOTE:} The base class for Filters is called \code{EventFilter} so that it |
| 38 |
doesn't conflict with \code{\link[base:funprog]{base::Filter()}}. The recommended convention for |
| 39 |
Filter subclasses is to call them \code{FilterSomething} and leave out the |
| 40 |
\code{Event} prefix. |
| 41 |
} |
| 42 |
|
| 43 |
\examples{ |
| 44 |
lg <- get_logger("test") |
| 45 |
f <- function(event) { |
| 46 |
cat("via event$.logger:", event$.logger$threshold, "\n") # works for loggers only |
| 47 |
cat("via .obj(): ",.obj()$threshold, "\n") # works for loggers and appenders |
| 48 |
TRUE |
| 49 |
} |
| 50 |
lg$add_filter(f) |
| 51 |
lg$fatal("test") |
| 52 |
lg$config(NULL) |
| 53 |
} |
| 54 |
\seealso{ |
| 55 |
\code{\link[=is_filter]{is_filter()}} |
| 56 |
} |
| 57 |
\section{Methods}{ |
| 58 |
\subsection{Public methods}{ |
| 59 |
\itemize{ |
| 60 |
\item \href{#method-EventFilter-new}{\code{EventFilter$new()}} |
| 61 |
\item \href{#method-EventFilter-clone}{\code{EventFilter$clone()}} |
| 62 |
} |
| 63 |
} |
| 64 |
\if{html}{\out{<hr>}} |
| 65 |
\if{html}{\out{<a id="method-EventFilter-new"></a>}} |
| 66 |
\if{latex}{\out{\hypertarget{method-EventFilter-new}{}}} |
| 67 |
\subsection{Method \code{new()}}{ |
| 68 |
Initialize a new EventFilter |
| 69 |
\subsection{Usage}{ |
| 70 |
\if{html}{\out{<div class="r">}}\preformatted{EventFilter$new(fun = function(event) TRUE)}\if{html}{\out{</div>}} |
| 71 |
} |
| 72 |
|
| 73 |
\subsection{Arguments}{ |
| 74 |
\if{html}{\out{<div class="arguments">}} |
| 75 |
\describe{ |
| 76 |
\item{\code{fun}}{a \code{function} with a single argument \code{event} that must return |
| 77 |
either \code{TRUE} or \code{FALSE}. Any non-\code{FALSE} will be interpreted as |
| 78 |
\code{TRUE} (= no filtering takes place) and a warning will be thrown.} |
| 79 |
} |
| 80 |
\if{html}{\out{</div>}} |
| 81 |
} |
| 82 |
} |
| 83 |
\if{html}{\out{<hr>}} |
| 84 |
\if{html}{\out{<a id="method-EventFilter-clone"></a>}} |
| 85 |
\if{latex}{\out{\hypertarget{method-EventFilter-clone}{}}} |
| 86 |
\subsection{Method \code{clone()}}{ |
| 87 |
The objects of this class are cloneable with this method. |
| 88 |
\subsection{Usage}{ |
| 89 |
\if{html}{\out{<div class="r">}}\preformatted{EventFilter$clone(deep = FALSE)}\if{html}{\out{</div>}} |
| 90 |
} |
| 91 |
|
| 92 |
\subsection{Arguments}{ |
| 93 |
\if{html}{\out{<div class="arguments">}} |
| 94 |
\describe{ |
| 95 |
\item{\code{deep}}{Whether to make a deep clone.} |
| 96 |
} |
| 97 |
\if{html}{\out{</div>}} |
| 98 |
} |
| 99 |
} |
| 100 |
} |