简体   繁体   English

Boost.Log - 如何使用追加和旋转配置 text_file_backend

[英]Boost.Log - how to configure text_file_backend with append and rotate

How to create a text_file_backend that appends to existing file but also has rotation?如何创建一个附加到现有文件但也有旋转的 text_file_backend? I do this, but a new file is created every time I run my program.我这样做了,但是每次运行我的程序时都会创建一个新文件。

  1. App.log
  2. App.log00000
  3. App.log00001
  4. and so on and so forth ....等等等等......

     boost::log::add_file_log ( boost::log::keywords::auto_flush = true, boost::log::keywords::target = "Log", boost::log::keywords::file_name = "App.log", // file name pattern eg: keywords::file_name = "app%m%d%Y_%H%M%S_%5N.log", boost::log::keywords::open_mode = std::ios::out | std::ios::app, // append mode boost::log::keywords::rotation_size = 10 * 1024 * 1024, // rotate files every 10 MBytes boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), // ...or at midnight .....

Is there something else I can try?还有什么我可以尝试的吗?

Thanks谢谢

With the target parameter you configured the target directory to be different from the directory where the log file is initially written (the file_name parameter), which means on rotation the file will be moved.使用target参数,您将目标目录配置为与最初写入日志文件的目录( file_name参数)不同,这意味着文件将在轮换时移动。 Since the file name contains no placeholders that could differentiate the files, the new file clashes with the ones left in the target directory from previous rotations, and a counter suffix is appended to avoid the conflict.由于文件名不包含可以区分文件的占位符,因此新文件与之前轮换后留在目标目录中的文件发生冲突,并附加了一个计数器后缀以避免冲突。

Appending only works when the sink backend opens the file identified by file_name and it already exists.仅当接收器后端打开由file_name标识的文件并且它已经存在时,附加才有效。 It does not make the file appended to an existing file in the target directory on rotation, this is not possible.它不会在旋转时将文件附加到目标目录中的现有文件,这是不可能的。 Since in your case the rotation moves the file away every time, appending will never happen.由于在您的情况下每次旋转都会将文件移开,因此永远不会发生追加。

In order for appending and rotation both to work you have to ensure that (a) the previous file written by the sink backend is left in the same directory and has the same name as the new sink tries to open and (b) the file name contains some placeholders so that on every rotation a new file name is generated.为了使附加和旋转都起作用,您必须确保 (a) 接收器后端写入的先前文件保留在同一目录中,并且与新接收器尝试打开的名称相同,以及 (b) 文件名包含一些占位符,以便在每次旋转时生成一个新的文件名。 These are conflicting requirements but some configurations can meet them.这些是相互冲突的要求,但某些配置可以满足它们。 For example, you can configure the target directory to be the same as where the log file is written by the backend, the file name to contain a date and rotate each day on midnight.例如,您可以将目标目录配置为与后端写入日志文件的位置相同,文件名包含日期并每天在午夜轮换。

boost::log::add_file_log
(
    boost::log::keywords::auto_flush          = true,
    boost::log::keywords::target              = "Log",        
    boost::log::keywords::file_name           = "Log/App_%Y%m%d.log",
    boost::log::keywords::open_mode           = std::ios::out | std::ios::app,
    boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0)
);

Note that file counters and rotation on file size do not work well with appending because they tend to violate one of the requirements I listed.请注意,文件计数器和文件大小轮换在追加时效果不佳,因为它们往往违反我列出的要求之一。

You need to set the keywords::enable_final_rotation to false (example here ) in order to not rotate when your process exits.您需要将keywords::enable_final_rotation设置为false此处的示例),以便在您的进程退出时不旋转。
This way, the next time your process start up, if the file was not rotated because of size / time rules, it will append to it.这样,下次您的进程启动时,如果文件由于大小/时间规则而未旋转,它将附加到它。

Also, I would suggest to set keywords::target_file_name with a pattern (eg "App_%Y%m%d_%H%M%S_%5N.log" ) so that the rotated files have distinct names (containing the date and an index in this case).另外,我建议使用模式(例如"App_%Y%m%d_%H%M%S_%5N.log" )设置"App_%Y%m%d_%H%M%S_%5N.log" keywords::target_file_name ,以便旋转的文件具有不同的名称(包含日期和在这种情况下的索引)。
If you don't do this, you will have max 1 rotated file (Log/App.log) which will be overwritten by the next rotated file.如果您不这样做,您将拥有最多 1 个旋转文件 (Log/App.log),这些文件将被下一个旋转文件覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM