【问题标题】:Boost.Log - how to configure a text sink backend to append to rotated filesBoost.Log - 如何配置文本接收器后端以附加到旋转文件
【发布时间】:2012-01-15 04:14:45
【问题描述】:

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

myLog001.log、myLog002.log 等

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

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

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

【问题讨论】:

    标签: c++ logging boost boost-log


    【解决方案1】:

    该功能内置于文本接收器中,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-&gt;scan_for_files();,如文档中“管理旋转的文件”标题下所示,但这只会防止库在需要清理之前覆盖以前的日志。

    当这个话题在 2013 年 2 月的开发邮件列表中出现时,该库的作者解释说 adding support for appending would be a nontrivial change 在当前设计下无法实现。

    【讨论】:

    • 我使用后端->scan_for_files();在我的程序中,但是在运行此代码时程序崩溃了。对此有任何想法吗?
    • 旋转有效,但是追加呢?对于程序的每次后续运行,程序应附加到最后一个日志,直到日志文件达到轮换限制。此代码在每次运行时都会创建一个新的日志文件,添加 scan_for_files 没有任何效果。
    【解决方案2】:

    您必须在使用文本文件之前指定 open_mode。默认情况下,Boost.Log 将使用 std::ios_base::trunc|std::ios_base::out 作为打开模式,这显然会截断旧的日志文件。

    您可以使用以下参数创建 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 2021-07-29
      • 1970-01-01
      • 2018-10-03
      相关资源
      最近更新 更多