Boost.Log - 如何配置文本接收器后端以附加到旋转的文件

Leo*_*Leo 22 c++ logging boost boost-log

我有一个sinks::text_file_backend水槽.说我已经有一些旋转的日志文件:

myLog001.log,myLog002.log等

我希望接收器继续写入最后一个旋转的文件 - myLog002.log,附加到其内容并从那里继续旋转.

我只是设法找到,keywords::open_mode = append但这只是附加在现有的myLogX文件之上,使它们更大,当然很难阅读.

可以在Boost.Log中完成吗?

Rob*_*edy 14

该功能内置于文本接收器,文档中包含一个示例,用于设置文件名模式以及以特定大小和时间旋转的规则:

// The function registers file sink in the logging library
void init_logging()
{
    boost::shared_ptr< logging::core > core = logging::core::get();

    boost::shared_ptr< sinks::text_file_backend > backend =
        boost::make_shared< sinks::text_file_backend >(
            // file name pattern
            keywords::file_name = "file_%5N.log",
            // rotate the file upon reaching 5 MiB size...
            keywords::rotation_size = 5 * 1024 * 1024,
            // ...or at noon, whichever comes first
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0)
        );

    // Wrap it into the frontend and register in the core.
    // The backend requires synchronization in the frontend.
    typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
    boost::shared_ptr< sink_t > sink(new sink_t(backend));

    core->add_sink(sink);
}
Run Code Online (Sandbox Code Playgroud)

显然没有办法使用此设置使库附加到现有文件.您应该backend->scan_for_files();在构建之前调用sink,如文档中的"管理旋转文件"标题所示,但这只会阻止库在它们到期之前覆盖以进行清理.

当该主题在2013年2月的开发邮件列表中出现时,该库的作者解释说,添加对附加的支持将是一个无法在当前设计下进行的重大更改.

  • @WaterLin,请参阅:[为什么我的应用程序在使用文件接收器时终止进程终止?](http://www.boost.org/doc/libs/1_57_0/libs/log/doc/html/log/rationale/ why_crash_on_term.html) (2认同)