Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my format doesn't work in boost log

I am using boost::log in this function:

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

void InitLog() {
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);
  logging::add_file_log(
            keywords::file_name = AppHolder::Instance().config().log_folder + "/sign_%Y-%m-%d_%H-%M-%S.%N.log",
            keywords::rotation_size = 10 * 1024 * 1024,
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
            keywords::format = "%TimeStamp% (%LineID%) <%Severity%>: %Message%"
            );
}

Then, given the call:

BOOST_LOG_TRIVIAL(info) << "thread id: " << this_thread::get_id() << " Initialization succeeded";

Output is not as expected: timestamp, line id and severity are empty.

() <>: thread id: 7f58e30e8740 Initialization succeeded

like image 274
Dean Chen Avatar asked Dec 03 '13 13:12

Dean Chen


People also ask

Is boost log thread safe?

The lg will refer to the one and only instance of the logger throughout the application, even if the application consists of multiple modules. The get function itself is thread-safe, so there is no need in additional synchronization around it.

What is boost log?

Log. Boost. Log is the logging library in Boost.


2 Answers

You have to register these attributes with the log core. Like this:

boost::log::add_common_attributes();

Or manually:

boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
// etc...
like image 154
Igor R. Avatar answered Sep 20 '22 04:09

Igor R.


After adding this, the problem was solved.

boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");

Full code is located at my blog use boost log step 4

like image 27
Dean Chen Avatar answered Sep 17 '22 04:09

Dean Chen