Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default formatting with boost::log::BOOST_TRIVIAL_LOG?

Tags:

c++

boost

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.

like image 445
Martin Avatar asked Jun 10 '12 22:06

Martin


1 Answers

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.

like image 186
Mehrwolf Avatar answered Nov 03 '22 15:11

Mehrwolf