【问题标题】:boost log text_file_backend performance提升日志 text_file_backend 性能
【发布时间】:2017-02-23 09:23:57
【问题描述】:

我正在使用 boost log 并且我选择了 text_file_backend,但是我的性能很差。无论是同步还是异步,boost 日志的性能都很低。大约在 6 秒内,它将 30M 数据写入日志文件。 下面是我的代码sn-p,有人可以帮帮我吗?

    typedef boost::log::sinks::asynchronous_sink<
      boost::log::sinks::text_file_backend> TextSink; 
    boost::log::sources::severity_logger_mt<LogSev> logger_;
    boost::shared_ptr<TextSink> report_sink_;

    // initialize report_sink
    boost::shared_ptr<sinks::text_file_backend> report_backend =
        boost::make_shared<sinks::text_file_backend>(
            keywords::file_name = target + "/" + file_name
                + ".report.log.%Y_%m_%d.%N",
            keywords::rotation_size = file_size, keywords::time_based_rotation =
                sinks::file::rotation_at_time_point(0, 0, 0),
            keywords::auto_flush = false);

    boost::shared_ptr<sinks::file::collector> report_collector = CreateCollector(
        target, max_use_size / 2, min_free_size);
    report_backend->set_file_collector(report_collector);
    report_backend->scan_for_files();

    // add sink: report_sink
    report_sink_ = boost::make_shared<TextSink>(report_backend);
    report_sink_->set_formatter(
        expr::format("[%1%]" + sep + "[%2%]" + sep + "[%3%]" + sep + "%4%")
            % expr::format_date_time<boost::posix_time::ptime>(
                "TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
            % expr::attr<LogSev>("Severity")
            % expr::attr<attrs::current_thread_id::value_type>("ThreadID")
            % expr::message);
    report_sink_->set_filter(expr::attr<LogSev>("Severity") >= report);
    logging::core::get()->add_sink(report_sink_);

    logging::add_common_attributes();

    BOOST_LOG_SEV(logger_, info) << "blabal...";

【问题讨论】:

    标签: performance boost rotation backend boost-log


    【解决方案1】:

    我认为关于您的实现的一个性能问题是关于 Timestamp。它需要一个系统调用来查找时间。我遇到了同样的问题。所以我转而使用date 库。它返回 UTC 时间非常快。还要检查this 问题的第一个答案,但是如果你想要基于时区的时间戳,日期库很慢。您应该更好地定义您的时区并添加到 UTC。

    看例子:

    #include "date.h"
    
    #define MY_TIME std::chrono::hours(4) + std::chrono::minutes(30)
    
    string timestamp = date::format("%F %T", std::chrono::system_clock::now() + 
                                    MY_TIME);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多