I am new to Boost Log, and having troubles doing some pretty simple stuff. I'm trying to create a logger and assign a level to it (such as Warning, Info, Trace, etc.), and filter out(for performance reasons) any logs sent to this logger with a lower level of that assigned to the logger, at the Logging core level, rather than at the sink level. For example (pseudo code):
logger lg;
lg.setLevel(Warn);
BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; // Will be filtered
BOOST_LOG_TRIVIAL(warn) << "A warning severity message"; // Won't be filtered
I'm pretty sure this can be achieved with Boost Log, but for some reason I was not able to do this.
Thanks,
Omer.
There are no filters on the logger (source) level in Boost.Log. You can set filters either globally (in the logging core) or per sink, and log records from all sources will be processed uniformly.
You can implement the behavior you want with use of channels by having each logger assigned a channel and filtering based on channel and severity level.
BOOST_LOG_ATTRIBUTE_KEYWORD(a_severity, "Severity", LogSeverity)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string)
typedef sources::severity_channel_logger< LogSeverity, std::string > logger_type;
logger_type lg_a(keywords::channel = "A");
logger_type lg_b(keywords::channel = "B");
core::get()->set_filter
(
(a_channel == "A" && a_severity >= LogSeverity::info) ||
(a_channel == "B" && a_severity >= LogSeverity::warning)
);
The library also provides a specialized filter that can be used to simplify this.
auto min_severity = expressions::channel_severity_filter(a_channel, a_severity);
min_severity["A"] = LogSeverity::info;
min_severity["B"] = LogSeverity::warning;
core::get()->set_filter(min_severity);
Note that you are actually not limited to have only one logger per channel - you can create multiple ones and log records from each will be treated the same way.