提升日志选择目标文件

Boost log select destination file

本文关键字:目标 文件 选择 日志      更新时间:2023-10-16

是否可以使用一个 Boost 日志实例登录 severeal 文件。

我的意思是是否可以指定日志将写入哪个文件:

BOOST_LOG_..(...) << "aaa" <- go to **A.log**
BOOST_LOG_..(...) << "bbb" <- go to **B.log**

是的,这是可能的 - 使用过滤器。

如何执行此操作完全取决于您的首选项,但下面是一个包含作用域记录器标记的示例:

void SomeFunction()
{
    {
        // everything in this scope gets logged to A.log
        BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Log", std::string, "LogA")
        BOOST_LOG(lg) << "aaa";
        BOOST_LOG(lg) << "aaa2";
    }
    {
        // everything in this scope gets logged to B.log
        BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Log", std::string, "LogB")
        BOOST_LOG(lg) << "bbb";
        BOOST_LOG(lg) << "bbb2";
    }
}
// This is your log initialization routine
void InitLogs()
{
    // Initialize sinkA to use a file backend that writes to A.log and sinkB to B.log.
    // ...
    // ...
    // Make sink A only accept records with the Log attribute "LogA"
    // while sink B will only accept records where it is "LogB".
    sinkA.set_filter(flt::attr<std::string>("Log") == "LogA");
    sinkB.set_filter(flt::attr<std::string>("Log") == "LogB");
}