I have a sinks::text_file_backend
sink. Say I already have a few rotated log files:
myLog001.log, myLog002.log and so on
I want the sink to keep writing to the last rotated file - myLog002.log, append to its contents and continue rotation from there on.
I have only managed to findkeywords::open_mode = append
but this only appends on top of the existing myLogX files, making them larger and of course very hard to read.
Can this be done in Boost.Log?
You have to specify the open_mode before use the text file. By default Boost.Log will use std::ios_base::trunc|std::ios_base::out as the open mode which obviously will truncate the old log file.
You can create text_file_backend instance with the following parameters:
That functionality is built in to the text sink, and the documentation includes an example to set the file-name pattern and rules for rotating at certain sizes and times:
There is apparently no way to make the library append to existing files with this setup. You should call
backend->scan_for_files();
prior to constructingsink
, as shown under the "Managing rotated files" heading in the documentation, but that only prevents the library from overwriting previous logs before they're due for cleanup.When this topic arose on a development mailing list in February 2013, the library's author explained that adding support for appending would be a nontrivial change that couldn't be made under the current design.