【问题标题】:boost 1.62 - log v2, a file rotation doesn't work when open mode is set to append databoost 1.62 - 日志 v2,当打开模式设置为附加数据时文件轮换不起作用
【发布时间】:2016-11-30 15:17:40
【问题描述】:

我使用 boost 1.62。我以这种方式初始化增强记录器:

logging::add_file_log
(
    keywords::file_name = "myfile.log",
    keywords::rotation_size = 1024,
    keywords::format = expr::stream
    << "["
    << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
    << "] <" << expr::attr< severity_level >("Severity")
    << "> " << expr::message

);

logging::add_common_attributes();

对于上述代码轮换工作正常,但是当程序启动时,会从一开始就创建一个日志文件。当我添加:

keywords::open_mode = (std::ios::out | std::ios::app)

add_file_log 新日志附加到现有日志文件,但轮换不起作用。我需要一个旋转的日志文件。当文件已存在时,应附加新数据。如何解决?

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    您应该在配置记录器时使用scan_matching。参见例如

    Boost::Log and log numbering

    #define BOOST_LOG_DYN_LINK 1
    #include <boost/log/core.hpp>
    #include <boost/log/common.hpp>
    #include <boost/log/attributes.hpp>
    #include <boost/log/sinks.hpp>
    #include <boost/log/expressions.hpp>
    #include <boost/log/trivial.hpp>
    #include <boost/log/utility/setup/common_attributes.hpp>
    #include <boost/log/utility/setup/file.hpp>
    #include <string>
    
    void initLogging()
    {
        boost::log::add_common_attributes();
        auto core = boost::log::core::get();
    
        core->add_global_attribute("UTCTimeStamp",boost::log::attributes::utc_clock());
    
        auto x = boost::log::add_file_log(
                boost::log::keywords::file_name             = "Log_%3N.log",
                boost::log::keywords::rotation_size         = 1 * 1024, // 1k
                boost::log::keywords::target                = "Logs",
                boost::log::keywords::min_free_space        = 30 * 1024 * 1024,
                boost::log::keywords::max_size              = 20 * 1024,
                boost::log::keywords::time_based_rotation   = boost::log::sinks::file::rotation_at_time_point(boost::gregorian::greg_day(31)),
                boost::log::keywords::scan_method           = boost::log::sinks::file::scan_matching,
                boost::log::keywords::format                = "%UTCTimeStamp% (%TimeStamp%) [%ThreadID%]: %Message%",
                boost::log::keywords::auto_flush            = true
            );
    
        //auto d = x->locked_backend()->scan_for_files();
    }
    
    int main()
    {
        initLogging();
        for (int i = 0; i < 20; ++i) {
            BOOST_LOG_TRIVIAL(trace) << "Let's go shopping " << std::string(400, '*');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2012-05-24
      • 1970-01-01
      • 2014-11-20
      • 2011-07-10
      • 2021-09-30
      • 2014-02-01
      相关资源
      最近更新 更多