【问题标题】:incorrect Boost Log timestamp format不正确的 Boost Log 时间戳格式
【发布时间】:2013-09-13 08:21:37
【问题描述】:

我正在使用 Boost 1.54 Log,并通过以下方式初始化我的日志记录:

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;

BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)

logging::add_common_attributes();
boost::shared_ptr<logging::core> core = logging::core::get();

// setup console log
logging::add_console_log (
    std::clog,
    keywords::filter = severity >= debug,
    keywords::format = (
        expr::stream << expr::format_date_time(timestamp, "%Y-%m-%d %H:%M:%S") <<
            line_id << " [" << severity << "] " << expr::smessage
    )
);

这是生成输出的示例:

2013-09-13 10:17:471 [warn] You are running in debug mode - assertions are enabled.
2013-09-13 10:17:472 [info] loading data completed
2013-09-13 10:17:473 [debug] applying xxx completed
2013-09-13 10:17:474 [debug] computing xxx completed
2013-09-13 10:17:475 [debug] xxx completed
2013-09-13 10:17:476 [info] xxx completed
2013-09-13 10:17:477 [debug] xxx completed
2013-09-13 10:17:478 [debug] computing xxx completed
2013-09-13 10:17:479 [info] testing xxx completed
2013-09-13 10:17:4710 [info] xxx
2013-09-13 10:17:4711 [debug] xxx completed

时间戳的输出秒数完全不正确,但根据 Boost Log 文档%S 是正确的秒数格式设置。这会是一个错误吗?

【问题讨论】:

  • 请添加包含的标题以让其他人使用 MWE。我偶然发现了namespace definition not allowedBOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)

标签: c++ logging boost boost-log


【解决方案1】:

时间和line_id 之间没有空格,所以它们一起运行。

【讨论】:

    【解决方案2】:

    否则用以下代码替换keywords::format:

     /*< log record format >*/
        keywords::format =
        (
            expr::stream
            //<< std::hex   //To print the LineID in Hexadecimal format
            << std::setw(8) << std::setfill('0') 
            << expr::attr< unsigned int >("LineID")
            << "\t"
            << expr::format_date_time<boost::posix_time::ptime>("TimeStamp","%H:%M:%S.%f")
            << "\t: <" << logging::trivial::severity
            << "> \t" << expr::smessage
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 2015-11-19
      相关资源
      最近更新 更多