【问题标题】:Forwarding Boost Log to File drops Meta-Informations将 Boost 日志转发到文件会丢弃元信息
【发布时间】:2013-10-31 16:40:39
【问题描述】:

我正在使用这样的 boost-log 库:

BOOST_LOG_TRIVIAL(info) << "My sample output";

我希望,我不需要更改它。

输出如下所示

[2013-10-31 17:19:19.044701] [0x3f1e6740] [info]    My sample output

问题:

我正在尝试将其从标准输出转发到文件。

当我尝试这样的事情时

boost::log::add_file_log("sample.log", boost::log::keywords::auto_flush = true);
boost::log::add_common_attributes();

BOOST_LOG_TRIVIAL 的输出一模一样

My sample output

我可以做些什么来找回我心爱的元信息?

【问题讨论】:

    标签: c++ logging boost


    【解决方案1】:

    在花了很多时间尝试之后,我自己想通了。

    我只是添加了

    boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
    boost::log::add_common_attributes();
    boost::log::add_file_log("sample.log",
                             boost::log::keywords::auto_flush = true,
                             boost::log::keywords::format = "[%TimeStamp%] [%ProcessID%] [%ThreadID%] [%Severity%]: %Message%" );
    

    作为第一行之一。这会将日志写入 sample.log

    关键似乎是add_file_log 覆盖了BOOST_LOG_TRIVIAL 的样式。添加我自己的boost::log::keyword::format 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 2015-10-11
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多