![](/img/trans.png)
[英]Boost.Log - how to configure text_file_backend with append and rotate
[英]Boost.Log - how to configure a text sink backend to append to rotated files
我有一個sinks::text_file_backend
器。 假設我已經有一些輪換的日志文件:
myLog001.log,myLog002.log等
我希望接收器繼續寫入最后一個旋轉的文件-myLog002.log,追加到其內容並從那里繼續旋轉。
我只能設法找到keywords::open_mode = append
但這只能附加在現有myLogX文件的頂部,這會使它們變大,當然也很難閱讀。
可以在Boost.Log中完成嗎?
該功能內置在文本接收器中,並且文檔中包含一個示例,用於設置文件名模式和以特定大小和時間旋轉的規則:
// 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);
}
使用此設置,顯然無法將庫附加到現有文件中。 您應該調用backend->scan_for_files();
在構造接收sink
之前,如文檔中“管理旋轉的文件”標題下所示,但這僅可防止庫在需要清理之前覆蓋先前的日志。
當這個話題在2013年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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.