Boost.Log filters beforehand; therefore, make_trace_record()
will not be called if the severity is not high enough.
In order to set the severity filter for the trivial logger, call:
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= boost::log::trivial::...
);
For instance, the following example outputs 1
, showing that expensive()
is only called once:
Live On Coliru
#include <iostream>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
int count = 0;
int expensive()
{
return ++count;
}
int main()
{
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= boost::log::trivial::warning
);
BOOST_LOG_TRIVIAL(error) << expensive();
BOOST_LOG_TRIVIAL(info) << expensive();
std::cout << count << '\n';
return 0;
}
Prints:
[2018-05-21 14:33:47.327507] [0x00007eff37aa1740] [error] 1
1
For those wondering how it works, take a look to: How does the "lazy evaluation" of Boost Log's trivial loggers work?