![]() |
While severity levels can be used for informative purposes, you will normally want to apply filters to write only significant records to the file and ignore the rest. It is easy to do so by setting a global filter in the library core, like this:
#include <boost/log/core.hpp> #include <boost/log/trivial.hpp> #include <boost/log/filters.hpp> void init() { logging::core::get()->set_filter ( flt::attr< logging::trivial::severity_level >("Severity") >= logging::trivial::info ); } int main(int, char*[]) { init(); BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; }
Now, if we run this code sample, the first two log records will be ignored, while the remaining four will pass on to the file.
![]() |
Important |
---|---|
Remember that the streaming expression is only executed if the record passed filtering. Don't specify business-critical calls in the streaming expression, as these calls may not get invoked if the record is filtered away. |
A few words must be said about the filter setup expression. Since we're setting
up a global filter, we have to acquire the logging core instance. This is
what logging::core::get()
does
- it returns a pointer to the core singleton. The set_filter
method of the logging core sets the global filtering function.
The filter is built as a lambda expression. In our case, this expression
consists of a single logical predicate, whose left argument is a placeholder
that describes the attribute to be checked, and the right argument is the
value to be checked against. The flt::attr
is a template function that accepts attribute value type as its template
parameter, and the attribute name as its first argument. In other words,
the filter passes the record if it has attribute value with name "Severity"
and type severity_level
,
and the value is not less than info
.
Pretty easy, isn't it?
It is possible to build more complex filters as lambda expressions, combining logical predicates like this with each other, or even define your own function that would act as a filter. More on this is said here.