Boost C++ Libraries

PrevUpHomeNext

Step 5: Filtering

And now we have come to filtering log records. Filtering takes place before any formatting is done and is performed for each log record attempted to be written. Therefore a careful choice of filters is very critical for the performance of logging and, of course, for the amount of information left in logs. The library offers two phases of filtering: global and sink-based. The global filtering takes place prior to the sink-based and is intended to provide a way to globally disable or enable logging or to drop all low-priority and unneeded records. The sink-based filtering is a more fine tuning. Sink-specific filters basically decide which particular records will be processed by the particular sink.

Both phases of filtering are implemented the same way. The filter is a function object that supports the following signature:

bool (attribute_values_view const& values);

As you can see, filtering is only possible by analyzing attribute values of each record. It is not possible to analyze record message text since it is not constructed at the point of filtering. However, if needed, it is possible to perform late filtering in the sink implementation after the formatting is completed, but because of obvious performance loss of such approach it is not supported by the library sinks out of box.

The filter function object should return true if the record have passed the filtering (IOW, it should be written to log) and false otherwise. Like with formatters, it is possible to write your own filter from scratch or make use of a lambda-like syntax to generate one.

// Setting global filter
logging::core::get()->set_filter(
    flt::attr< severity_level >("Severity") >= warning); // Write all records with "warning" severity or higher

// Setting a sink-specific filter
pSink->set_filter(
    flt::has_attr("Tag") && // The sink will only write records that have an attribute "Tag"...
    flt::attr< std::string >("Tag").begins_with("LOGME")); // ...whose value begins with word "LOGME"

The syntax is quite similar to formatters. The difference is that there is no need in a hook placeholder like stream. You may see a more detailed description of filters in the Detailed features description section.


PrevUpHomeNext