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
     ));
}
  • 1
    I don't know how to do this, but log4cpp does all of this and is widely used. http://log4cpp.sourceforge.net/api/classlog4cpp_1_1RollingFileAppender.html – Steve Townsend May 14 '12 at 10:43
  • possible duplicate of [Boost.Log - how to configure a text sink backend to append to rotated files](http://stackoverflow.com/questions/8418917/boost-log-how-to-configure-a-text-sink-backend-to-append-to-rotated-files) – Rob Kennedy Jan 19 '14 at 15:32

1 Answers1

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
        ));
Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
  • 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 '14 at 05:01