【问题标题】:why my format doesn't work in boost log为什么我的格式在 boost 日志中不起作用
【发布时间】:2013-12-03 13:33:02
【问题描述】:

我在这个函数中使用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 初始化成功

【问题讨论】:

    标签: c++ boost boost-log


    【解决方案1】:

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

    boost::log::add_common_attributes();
    

    或手动:

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

    【讨论】:

      【解决方案2】:

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

      boost::log::r​​egister_simple_formatter_factory("Severity");

      完整代码位于我的博客 use boost log step 4

      【讨论】:

      • 在链接的博客文章中需要注意的一点:您似乎需要在调用add_file_log 之前调用register_simple_formatter_factory。 (可以说是显而易见的,但我还是把它搞砸了。)
      猜你喜欢
      • 2022-09-30
      • 2018-04-09
      • 1970-01-01
      • 2015-02-28
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多