3

I am using boost log and I choose the text_file_backend, but I got bad performance. No matter sync or async, the boost log has a low performance. About in 6 seconds, it wrote 30M data to log file. Follow is my code snippet, anyone can help me?

    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...";
Nature.Li
  • 345
  • 1
  • 3
  • 11

1 Answers1

0

I think one performance issue about your implementation is about Timestamp. It needs a system-call to find Time. I encountered the same problem. So I turned to use date library. It returns UTC time very fast. Also check first answer of this question, However if you want Timestamp based on timezone, date library is slow. you should better define your timezone and add to UTC.

see the example:

#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);
Jafar Gh
  • 137
  • 2
  • 12