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

标签 c++ logging boost boost-log

我有一个 sinks::text_file_backend 接收器。假设我已经有一些旋转的日志文件:

myLog001.log、myLog002.log 等

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

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

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

最佳答案

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

// 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();,如文档中“管理旋转文件”标题下所示,但这只会阻止库在需要清理之前覆盖以前的日志。

当这个话题在 2013 年 2 月的开发邮件列表中出现时,图书馆的作者解释说 adding support for appending would be a nontrivial change在目前的设计下是做不到的。

关于c++ - Boost.Log - 如何配置文本接收器后端以附加到旋转文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8418917/

相关文章:

c++ - lcov 问题 : weird duplicate constructor marked as not covered & function not marked as covered, 即使其行已被执行

c++ - 处理未知大小的输入

c++ - 安全地取消 boost asio 截止时间计时器

c++ - 停止 Thrift 服务器(TSimpleServer)

c++ - 为什么我不能将 std::unordered_map 或 boost::unordered_map 与 boost::multiprecision 类型一起使用?

c# - 平台调用 #define 指令

c++ - 只有在一个参数中没有使用 std::function 时,模板函数替换才有效

python - 为什么控制台记录器与打印不同步?

python - 在使用时间旋转记录器创建的每个日志文件中写入一个标题

java - 为 Web 项目设置 log4j 输出文件 : issue with destination folder