提升日志选择目标文件

时间:2011-05-11 06:54:56

标签: c++ boost

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

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

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

1 个答案:

答案 0 :(得分:1)

是的,可以 - 使用filters

你如何做到这完全取决于你的偏好,但这里是一个带范围记录器标签的例子:

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");
}