c++ - Boost::Log - 使用严重性和自定义过滤器属性记录?使用哪个宏?

标签 c++ logging boost filter boost-log

我想使用 boost::log 让我的负载测试应用程序记录到不同的文件和控制台。每个工作线程(代表连接到要测试的服务器的一个用户)应记录线程日志并将失败的调用记录到失败的调用日志中。我尝试通过使用过滤器来实现这一点。

目标是:

=> All logs with severity = lower than "INFO" will be discarded
=> All log records having the attribute "global" go to ./logs/loadtest.log AND to console
=> All log records having the attribute "thread" go to ./logs/thread.log AND to console
=> All log records having the attribute "faileCalls" go to ./logs/failedCalls.log AND to console

这是我的初始化代码:

void initLogging()
{
boost::shared_ptr<boost::log::core::basic_core> core = boost::log::core::get();

// Construct the sinks for our global log file and for console
typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> text_sink;

// global log file
boost::shared_ptr<text_sink> sinkLoadtest = boost::make_shared<text_sink>();
sinkLoadtest->locked_backend()->add_stream(boost::make_shared<std::ofstream>("./logs/loadtest.log", std::ios::app));
sinkLoadtest->locked_backend()->auto_flush(true);
sinkLoadtest->set_filter(boost::log::filters::has_attr("global"));
core->add_sink(sinkLoadtest);

// console
boost::shared_ptr<text_sink> sinkConsole = boost::make_shared<text_sink>();
boost::shared_ptr<std::ostream> stream(&std::clog, boost::log::empty_deleter());
sinkConsole->locked_backend()->add_stream(stream);
sinkConsole->locked_backend()->auto_flush(true);
core->add_sink(sinkConsole);


// thread dependent logging (one file per workthread)
    sinkThread = boost::make_shared<text_sink>();
sinkFailedCalls = boost::make_shared<text_sink>();

    sinkThread->locked_backend()->add_stream(boost::make_shared<std::ofstream>("./logs/thread.log"), std::ios::app));
    sinkFailedCalls->locked_backend()->add_stream(boost::make_shared<std::ofstream>("./logs/failedCalls.log", std::ios::app));

    sinkThread->set_filter(boost::log::filters::has_attr("thread"));
    sinkFailedCalls->set_filter(boost::log::filters::has_attr("failedCalls"));

    core->add_sink(sinkThread);
    core->add_sink(sinkFailedCalls);

    // set global severity level
    core->set_filter(boost::log::filters::attr<boost::log::trivial::severity_level>("Severity") >= boost::log::trivial::info);
}

这些是我的问题: 要使用哪个宏才能通过严重级别以及我的自定义过滤器属性之一?

获取记录器的最佳方式是什么?总是从核心获取它还是有一个成员变量“logger”?我需要它是线程安全的。

预先感谢您的努力!

最佳 简

//编辑: 如果这样的宏提供使用流媒体运营商,那就太好了

最佳答案

您必须使用 BOOST_LOG_STREAM_WITH_PARAMS 或等效的 BOOST_LOG_WITH_PARAMS

关于c++ - Boost::Log - 使用严重性和自定义过滤器属性记录?使用哪个宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11328887/

相关文章:

mysql - Innodb 日志刷新以防止数据丢失

python - Pytest 日志记录忽略 pytest.ini 中的选项

Apache 将时间戳记录到 Date typescript

c++ - string::replace 在有效迭代器上抛出 std::out_of_range

c++ - Boost.Asio SSL 上下文未验证证书

c++ - 如何将 boost::polygon 中的 polygon_set_data 转换为 polygon_data?

C++ mktime 返回随机日期

c++ - 函数坏指针运行时错误?

c++ - VS2010的concurency runtime和unbounded_buffer<shared_ptr<T>>,有什么坑吗?

c++ - 在 C 或 C++ 中是否有用于矩阵计算的开源模板库?