How to redirect Boost.Log to file

11,011

You can make BOOST_LOG_TRIVIAL use a file. Note that most of the boost::log examples use a namespace alias as shown below.

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;

void init()
{
    logging::add_file_log("sample.log");

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
}

And in main:

int main(int, char*[])
{
    init();

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
 // other types of severity
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}
Share:
11,011
Elvis Dukaj
Author by

Elvis Dukaj

Updated on June 14, 2022

Comments

  • Elvis Dukaj
    Elvis Dukaj almost 2 years

    I want a simple log file in a concurrent application. I've download Boost.Log v2.0 and using compiled it with Boost 1.53.0.

    The problem is that Boost.Log output on console. I'm using BOOST_LOG_TRIVIAL(trace).

    Is there a nice way to redirect BOOST_LOG_TRIVIAL to a file?

  • Elvis Dukaj
    Elvis Dukaj about 11 years
    what header have I to include for logging::add_file_log("sample.log") ?
  • m0nhawk
    m0nhawk about 11 years
    @elvis.dukaj: #include <boost/log/utility/setup/file.hpp>