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

Boost.Log - how to configure a text sink backend to append to rotated files(Boost.Log - 如何配置文本接收器后端以附加到旋转文件)

本文介绍了Boost.Log - 如何配置文本接收器后端以附加到旋转文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 sinks::text_file_backend 接收器.假设我已经有一些轮换的日志文件:

I have a sinks::text_file_backend sink. Say I already have a few rotated log files:

myLog001.log、myLog002.log 等

myLog001.log, myLog002.log and so on

我希望接收器继续写入最后一个旋转的文件 - 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.

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

I have only managed to findkeywords::open_mode = append but this only appends on top of the existing myLogX files, making them larger and of course very hard to read.

这可以在 Boost.Log 中完成吗?

Can this be done in 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);
}

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

There is apparently no way to make the library append to existing files with this setup. You should call 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.

当这个话题出现在 2013 年 2 月的开发邮件列表中时,图书馆的作者解释说 添加对追加的支持将是一项重要的更改,在当前设计下无法进行.

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.

这篇关于Boost.Log - 如何配置文本接收器后端以附加到旋转文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐