2011-05-11 36 views
3

是否可以使用Boost日志的一个实例登录到严重文件。Boost日志选择目标文件

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

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

回答

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