Search code examples
c++boost-loglog-rotation

Boost rotate logs hourly


I am using boost::log as the logging engine for a service. I know how to set up the log rotation to be daily/weekly, but what I would like to set-up is an hourly rotation of the logs. I am unable to find any straight forward implementation in boost and I am thinking maybe someone has a workaround?

Also, is it possible to have rotated files created, even if there was no log entry for a specific hour?

use case:

log_file.log
13.06.2016 12:02 line1
13.06.2016 12:23 line2
13.06.2016 12:45 line3
13.06.2016 13:02 line4
13.06.2016 15:02 line5

I'd like the output files to be:

**log_file_13:06:2016-12**
13.06.2016 12:02 line1
13.06.2016 12:23 line2
13.06.2016 12:45 line3

**log_file_13:06:2016-13**
13.06.2016 13:02 line4

**log_file_13:06:2016-14**

**log_file_13:06:2016-15**
13.06.2016 15:02 line5

Any suggestions/code examples are much appreciated.


Solution

  • I suppose you're using boosts sink backends. It's possible to rotate hourly, stated from there:

    Time interval rotations: rotation_at_time_interval class. With this predicate the rotation is not bound to any time points and happens as soon as the specified time interval since the previous rotation elapses. This is how to make rotations every hour:

    sinks::file::rotation_at_time_interval(posix_time::hours(1))