4

I want to use boost::log to let my loadtest application log to different files and to console. Each workthread (representing one user connected to the server to be tested) shall log thread log and log failed calls to failed calls log. I try to achieve that by using filters.

The goal is:

=> 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

This is my initialization code:

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

These are my questions: Which macro(s) to use to be able to pass Severity level PLUS one of my custom filter attributes?

What is the best way to obtail the logger? Alwas get it from core or have a member variable "logger"? I need it to be thread-safe.

Thank you in advance for your effort!

Best Jan

// EDIT: It would be great if such a macro would offer to use streaming operators

Rip-Off
  • 358
  • 1
  • 4
  • 14

1 Answers1

0

You must use BOOST_LOG_STREAM_WITH_PARAMS or the equivalent BOOST_LOG_WITH_PARAMS

Loghorn
  • 2,729
  • 17
  • 22