我正在尝试按可配置的严重性级别创建过滤器。我写了以下过滤器类:
class FilterBySeverity
{
public:
FilterBySeverity(boost::log::trivial::severity_level logLevel) :
m_logLevel(logLevel),
m_severityAttributeName("Severity")
{
}
bool operator()(const boost::log::attribute_value_set& attrs) const noexcept
{
auto it = attrs.find(m_severityAttributeName);
if (it == attrs.end()) return true;
return it->second.extract<int>() >= m_logLevel;
}
private:
const boost::log::trivial::severity_level m_logLevel;
const boost::log::attribute_name m_severityAttributeName;
};
然后我初始化日志如下:
auto sink = boost::log::add_console_log(std::cout);
sink->set_filter(FilterBySeverity(logOptions.m_severity));
sink->locked_backend()->auto_flush(true);
然后我正在制作测试输出:
BOOST_LOG_TRIVIAL(info) << "Logging started.";
但我看不到我的消息。但是当我注释掉对 set_filter() 的调用时,会出现消息。logOptions.m_severity 的值为 0,即应允许所有消息。试图在 operator() 中设置断点并检查发生了什么 - 它似乎工作正常。我做错了什么?