为什么我的格式在提升日志中不起作用

why my format doesn't work in boost log

本文关键字:日志 不起作用 我的 格式 为什么      更新时间:2023-10-16

我在这个函数中使用boost::log

#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%"
            );
}

然后,接到电话:

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

输出不符合预期:时间戳行id严重性为空。

()<>:线程id:7f58e30e8740初始化成功

您必须向日志核心注册这些属性。像这样:

boost::log::add_common_attributes();

或手动:

boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
// etc...

添加这些之后,问题就解决了。

boost::log::register_simple_formatter_factory<boost::log::trial::severity_level,char>("严重性");

完整代码位于我的博客中使用提升日志步骤4