简体   繁体   English

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

[英]Boost.Log - how to configure a text sink backend to append to rotated files

I have a sinks::text_file_backend sink. 我有一个sinks::text_file_backend器。 Say I already have a few rotated log files: 假设我已经有一些轮换的日志文件:

myLog001.log, myLog002.log and so on myLog001.log,myLog002.log等

I want the sink to keep writing to the last rotated file - myLog002.log, append to its contents and continue rotation from there on. 我希望接收器继续写入最后一个旋转的文件-myLog002.log,追加到其内容并从那里继续旋转。

I have only managed to find keywords::open_mode = append but this only appends on top of the existing myLogX files, making them larger and of course very hard to read. 我只能设法找到keywords::open_mode = append但这只能附加在现有myLogX文件的顶部,这会使它们变大,当然也很难阅读。

Can this be done in Boost.Log? 可以在Boost.Log中完成吗?

That functionality is built in to the text sink, and the documentation includes an example to set the file-name pattern and rules for rotating at certain sizes and times: 该功能内置在文本接收器中,并且文档中包含一个示例,用于设置文件名模式和以特定大小和时间旋转的规则:

// 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);
}

There is apparently no way to make the library append to existing files with this setup. 使用此设置,显然无法将库附加到现有文件中。 You should call backend->scan_for_files(); 您应该调用backend->scan_for_files(); prior to constructing sink , as shown under the "Managing rotated files" heading in the documentation, but that only prevents the library from overwriting previous logs before they're due for cleanup. 在构造接收sink之前,如文档中“管理旋转的文件”标题下所示,但这仅可防止库在需要清理之前覆盖先前的日志。

When this topic arose on a development mailing list in February 2013, the library's author explained that adding support for appending would be a nontrivial change that couldn't be made under the current design. 当这个话题在2013年2月出现在开发邮件列表中时,该库的作者解释说, 添加对附加的支持将是不平凡的更改 ,在当前设计下是无法进行的。

You have to specify the open_mode before use the text file. 您必须在使用文本文件之前指定open_mode。 By default Boost.Log will use std::ios_base::trunc|std::ios_base::out as the open mode which obviously will truncate the old log file. 默认情况下,Boost.Log将使用std :: ios_base :: trunc | std :: ios_base :: out作为打开模式,这显然会截断旧日志文件。

You can create text_file_backend instance with the following parameters: 您可以使用以下参数创建text_file_backend实例:

    {
        boost::shared_ptr<sinks::text_file_backend> backend =
            boost::make_shared<sinks::text_file_backend>(
                keywords::file_name = logger_file_path,
                keywords::open_mode = std::ios_base::app|std::ios_base::out,
                keywords::rotation_size = 5 * 1024 * 1024,
                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));
        sink->set_formatter(logFmt);
        core->add_sink(sink);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM