我有一个 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/