Boost.Log中缺少时间戳

时间:2020-02-06 16:45:39

标签: c++ boost boost-log

我是Boost.Log的新手。我想以以下格式打印数据: [TimeStamp][Severity] Message

我有这样的代码

auto my_formatter = [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm)
{
    strm << "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]";
    strm << "[" << rec[boost::log::trivial::severity] << "] ";
    strm << rec[boost::log::expressions::smessage];
};

void addFileSink()
{
    using sinkType = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

    auto backend = boost::make_shared<boost::log::sinks::text_file_backend>(
        boost::log::keywords::file_name = "log_%N.log",
        boost::log::keywords::rotation_size = 10 * 1024 * 1024,
        boost::log::keywords::open_mode = std::ios_base::app
    );

    boost::shared_ptr<sinkType> sink(new sinkType(backend));
    sink->set_formatter(my_formatter);
    sink->set_filter(boost::log::trivial::severity >= boost::log::trivial::severity_level::info);
    boost::log::core::get()->add_sink(sink);
}

void addConsoleSink()
{
    using sinkType = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;

    auto backend = boost::make_shared<boost::log::sinks::text_ostream_backend>();
    backend->add_stream(boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter()));
    backend->auto_flush(true);

    boost::shared_ptr<sinkType> sink(new sinkType(backend));
    sink->set_formatter(my_formatter);
    sink->set_filter(boost::log::trivial::severity >= boost::log::trivial::severity_level::error);
    boost::log::core::get()->add_sink(sink);
}

int main()
{
    boost::log::add_common_attributes();

    addFileSink();
    addConsoleSink();

    boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;

    BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::error) << "Message error";
    BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::info) << "Message info";

    return 0;
}

在TimeStamp属性旁边工作正常,我得到这样的输出:

[[error] Message error

我尝试了与示例等不同的方法,但是找不到该问题的解决方案。 boost::log::add_common_attributes();没有帮助。

1 个答案:

答案 0 :(得分:1)

boost::log::expressions::format_date_time不是格式化日期和时间的功能。这是一个用于Lambda格式表达式的 formatter generator 。换句话说,format_date_time创建一个格式化程序(具有特定签名的功能对象),该格式化程序在调用时将从日志记录中提取属性值并将其格式化。

您应该使用Lambda表达式而不是my_formatter函数来构建格式化程序,在这种情况下,您可以使用format_date_time,或使用格式化日期和时间的传统方法之一,例如{ {3}}或strftime

使用lambda表达式的示例:

namespace expr = boost::log::expressions;

sink->set_formatter
(
    expr::stream
        << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
        << "[" << boost::log::trivial::severity << "] "
        << expr::smessage
);

使用strftime的示例:

auto my_formatter = [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm)
{
    if (auto timestamp = boost::log::extract< boost::posix_time::ptime >("TimeStamp", rec))
    {
        std::tm ts = boost::posix_time::to_tm(*timestamp);

        char buf[128];
        if (std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ts) > 0)
            strm << "[" << buf << "]";
    }

    strm << "[" << rec[boost::log::trivial::severity] << "] ";
    strm << rec[boost::log::expressions::smessage];
};