2

After adding append flag "keywords::open_mode = std::ios_base::app" to file sink the normal file rotation is not happening when the file reaches Max size, as specified in below code:

typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
        boost::shared_ptr< logging::core > core = logging::core::get();
        boost::shared_ptr< file_sink > sink(new file_sink(
            keywords::file_name =  "/tmp/test.log", // log file with full path
            keywords::open_mode = std::ios_base::app, // append mode set
            keywords::rotation_size = 5000000
            ));
        sink->locked_backend()->set_file_collector(sinks::file::make_collector(
            keywords::target = "/tmp/",   // log file & target have same dir
            keywords::max_size = 5000000,
            keywords::min_free_space = 100000
            ));
        sink->locked_backend()->scan_for_files();
        sink->locked_backend()->auto_flush(true);
        core->add_sink(sink);

Boost Log version: 1.59

Behavior observed: After every time the process using boost logger starts; the log messages are getting appended to existing log file instead of creating new log file. But when the log file reaches it max size then boost file rotation policy is not happening and new log file is getting created without moving old log file to target directory.

Expected behavior: The log file should be appended with log messages and when reaches max size it should be rotated properly.

Please let me know if there is any solution to this problem.

SGPJ
  • 25
  • 7
  • 1
    any update or solution? – SGPJ Dec 09 '15 at 08:54
  • Aside from the obviously incorrect `max_size` value, log file rotation works fine for me. The file name `test.log` does not contain any placeholders, so I get `test.log00000`, `test.log00001`, etc. in the `tmp` directory. – Andrey Semashev Dec 10 '15 at 09:01
  • Also, you should know that when log files are collected to a different directory from the one where the log file is originally written, you won't have the file appending after the process restart. The last file that is written by the process before termination is rotated before exiting the process, and the next process will create a new file in its place. File appending will only happen if the previous file did not move to a different directory. – Andrey Semashev Dec 10 '15 at 09:05
  • 1
    I tried your recommendation and I have edited the question with recent update; both log file & target having same directory and so the log messages got appended to same log after restarting the process. After reaching the max size the file got rotated by creating new test.log in /tmp dir but the old log file got deleted without getting saved as test.log00002 in /tmp dir. So rotation is not working while append option is set. – SGPJ Dec 10 '15 at 16:11
  • The `max_size` value now is equal to the `rotation_size` value. This means that not more than one full log file can be stored in the `tmp` directory. So the rotation is working - it removes the old file to free space for the new one. Also, the old file is not renamed on rotation, the new one is. See here: http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/log/detailed/sink_backends.html#log.detailed.sink_backends.text_file.managing_rotated_files – Andrey Semashev Dec 11 '15 at 23:06
  • is there any method similar to file sink: auto_flush(true) in syslog sink back end? – SGPJ Jan 07 '16 at 11:47
  • facing new issue with syslog sink: http://stackoverflow.com/questions/34654577/auto-flush-in-boost-syslog-sink-backend-boost-1-59 – SGPJ Jan 07 '16 at 11:59

1 Answers1

1

In your program, you give same rotation size and maximum size this may be a problem. So try by giving maximum size as 5000000 and rotation size as 500000 (maximum size / 10). So that you may have 9 or 10 files before reaching maximum size.

Another thing is maximum size you set here is for folder (i.e.: //tmp - folder). Once the files (all files) reaches the size of maximum then old file gets deleted and new file will be placed in that folder.

Another thing is you can set file name as test_%N.log. So that your rotated file gets new name as test_1.log, test_2.log, etc.

Refer this site : http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/sink_backends.html

techraf
  • 64,883
  • 27
  • 193
  • 198