How to change the default formatting with boost::l

2019-06-15 14:34发布

问题:

boost::log looks really powerful. It offers a BOOST_LOG_TRIVIAL macro for trivial logging. But how can I change the default formatting? It prints the timestamp by default, by I don't want it. Do you have any idea? It seems the only way is to define a new sink ex-novo and add it to the core, then you can call set_format() on the backend in case. But this is no "trivial" anymore.

回答1:

Boost.Log has a default sink, which is used as long as you do not provide your own sink. The following code snippet changes the format of the console-log by adding a new sink.

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/console.hpp>

int main()
{
    boost::log::add_console_log(std::cout, boost::log::keywords::format = ">> %Message%");
    BOOST_LOG_TRIVIAL(info) << "Hello world!";
}

Note that you have to add the log_setup library to your build i.e. do a

-lboost_log_setup -lboost_log

where the order of the libs is important.



标签: c++ boost