c++ - 为什么我的格式在 boost 日志中不起作用

标签 c++ boost boost-log

我在这个函数中使用了 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";

输出不符合预期:timestampline idseverity 为空。

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

最佳答案

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

boost::log::add_common_attributes();

或手动:

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

关于c++ - 为什么我的格式在 boost 日志中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352584/

相关文章:

c++ - 仅当 .h 位于特定目录中时,g++ 才会神秘地失败

c++ - 警告 : address of local variable 'buffer' returned

c++ - 使用boost从文件路径中提取直接父目录

c++ - Boost.Log 无法设置日志过滤器(未声明的标识符 'severity')

c++ - boost log non-const bitfield 编译错误(向后兼容问题)

c++ - map.find() 找不到关键 c++

c++ - 如何在用户数据中存储值类型?

c++ - 添加一个指向多个 boost::ptr_vector 列表的指针时崩溃

c++ - Boost MPL排序模板参数包

c++ - 将 boost::log::expressions::attr< std::string > 转换为 std::string