Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost log select destination file

Tags:

c++

boost

Is it possible with one instance of Boost log, to log into severeal files.

I mean is it possible to specify in which file the log will be written:

BOOST_LOG_..(...) << "aaa" <- go to **A.log**
BOOST_LOG_..(...) << "bbb" <- go to **B.log**
like image 734
Guillaume Paris Avatar asked Dec 05 '25 04:12

Guillaume Paris


1 Answers

Yes, it's possible - using filters.

How you do it exactly depends on your preferences, but here's an example with scoped logger tags:

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");
}
like image 152
Boaz Yaniv Avatar answered Dec 07 '25 19:12

Boaz Yaniv