Murrgon
Murrgon

Reputation: 395

How to setup boost.log to limit the number of log files

I'm trying to setup logging using Boost.Log (v1.55.0) and I can't seem to find a way to setup the file collector on the backend so it will only maintain the last 20 logs.

namespace sinks     = boost::log::sinks;
namespace keywords  = boost::log::keywords;

typedef sinks::text_file_backend            TextFileBackend;
typedef boost::shared_ptr<TextFileBackend>  TextFileBackendPtr;

TextFileBackendPtr pBackend =
  boost::make_shared<TextFileBackend>
  (
    keywords::file_name = "BoostLogTest_%Y%m%d.log",                            
    keywords::auto_flush = true
  );

// Set up where the rotated files will be stored
pBackend->set_file_collector
(
  sinks::file::make_collector
  (
    keywords::target = "..\\Logs"
  )
);

In the call to sinks::file::make_collector there are a number of keywords I have found like max_size and min_free_space, but both of those are not what I'm looking for. I just want something like max_files so I can tell it to only keep the last 20 logs, regardless of how much disk space they are taking up. The only reference I could find to something like this was this ticket that was opened: https://svn.boost.org/trac/boost/ticket/8746.

There also doesn't seem to be a documented list of keywords that are available to be used. All of the ones I have found have been from examples found on the net.

Upvotes: 8

Views: 6122

Answers (2)

cbuchart
cbuchart

Reputation: 11555

It is possible starting with version 1.61, using the max_files parameter: http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/detailed/sink_backends.html

void init_file_collecting(boost::shared_ptr< file_sink > sink)
{
    sink->locked_backend()->set_file_collector(sinks::file::make_collector(
        keywords::target = "logs",
        keywords::max_size = 16 * 1024 * 1024,
        keywords::min_free_space = 100 * 1024 * 1024,
        keywords::max_files = 512
    ));
}

Upvotes: 5

wonce
wonce

Reputation: 1921

From the documentation of make_collector, taken from text_file_backend.hpp:

The following named parameters are supported:

  • target - Specifies the target directory for the files being stored in. This parameter is mandatory.
  • max_size - Specifies the maximum total size, in bytes, of stored files that the collector will try not to exceed. If the size exceeds this threshold the oldest file(s) is deleted to free space. Note that the threshold may be exceeded if the size of individual files exceed the \c max_size value. The threshold is not maintained, if not specified.
  • min_free_space - Specifies the minimum free space, in bytes, in the target directory that the collector tries to maintain. If the threshold is exceeded, the oldest file(s) is deleted to free space. The threshold is not maintained, if not specified.

So boost::log currently does not support collecting old log files based on their number.

Upvotes: 6

Related Questions