提升日志未显示自定义时间戳 [英] Boost log not showing custom TimeStamp

查看:105
本文介绍了提升日志未显示自定义时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost日志库(版本1.55),并试图自定义输出格式.不幸的是,我似乎无法制作一种自定义格式来显示TimeStamp属性.

I'm working with the Boost log library (version 1.55) and am trying to customize the output format. Unfortunately I can't seem to make a custom format to show the TimeStamp attribute.

示例程序,主要来自Boost日志文档中的示例:

Example program, mostly taken from the examples in the Boost log documentation:

#include <boost/log/trivial.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/utility/empty_deleter.hpp>

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

int main()
{
    using text_sink = sinks::synchronous_sink<sinks::text_ostream_backend>;
    auto sink = boost::make_shared<text_sink>();

    // Add standard output as destination for the sink
    boost::shared_ptr<std::ostream> stream(&std::cout, boost::empty_deleter());
    sink->locked_backend()->add_stream(stream);

    // Set output format
    sink->set_formatter(
        expr::stream
            << '['
            << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
            << "] - "
            << expr::smessage
        );

    logging::core::get()->add_sink(sink);

    // Test logging
    BOOST_LOG_TRIVIAL(info) << "Hello world";
}

该程序的输出为


[] - Hello world

格式化有效,但TimeStamp部分应放在方括号内.

The formatting works, except the TimeStamp part which should be inside the square brackets.

我使用的格式化功能是否错误,还是我遗漏了其他内容?

Am I using the formatting functions wrong, or am I missing something else?

推荐答案

您应包括文件<boost/log/utility/setup/common_attributes.hpp>和调用函数

You should include file <boost/log/utility/setup/common_attributes.hpp> and call function

logging::add_common_attributes()

这篇关于提升日志未显示自定义时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆