why my format doesn't work in boost log
Asked Answered
H

2

8

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

Humber answered 3/12, 2013 at 13:33 Comment(0)
H
4

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

Humber answered 6/12, 2013 at 9:0 Comment(1)
One thing to notice in the linked blog post: it looks like you need to call register_simple_formatter_factory before the call to add_file_log. (Arguably obvious, but I messed it up anyway.)Charkha
R
18

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...
Recrement answered 4/12, 2013 at 15:1 Comment(0)
H
4

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

Humber answered 6/12, 2013 at 9:0 Comment(1)
One thing to notice in the linked blog post: it looks like you need to call register_simple_formatter_factory before the call to add_file_log. (Arguably obvious, but I messed it up anyway.)Charkha

© 2022 - 2024 — McMap. All rights reserved.