14

How to configure boost log, for writing with rotating and appending?

And after restart program, don't clear log file.

int init_log()
{
    typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
    boost::shared_ptr< file_sink > sink( new file_sink (
       keywords::file_name = "app.log", // only one files
       // keywords::open_mode = (std::ios::out | std::ios::app), // is not append
       keywords::open_mode = (std::ios::out | std::ios::app), // is not rotated
       keywords::rotation_size = 10 * 1024
     ));
}
2

1 Answer 1

8

Please try this out (the key is adding the log name using some pattern and not fixing its name like "app.log") this will enable flre rotation and the data will be appended to each created file. Regarding the log clearing, as long as you haven't specified custom file_collector, your logs should remain intact :)

typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
boost::shared_ptr< file_sink > sink( new file_sink(
        keywords::file_name     = "app%m%d%Y_%H%M%S_%5N.log",   // file name pattern
        keywords::rotation_size = 10*1024                       // rotation size, in characters
        ));
1
  • 8
    Log data should be appended to the same log file, for each program invocation, until rotation size is reached. With that code, every run creates a new log file.
    – ixe013
    Jan 19, 2014 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.