Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

logging

boost

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.

like image 914
Murrgon Avatar asked Jan 22 '14 16:01

Murrgon


People also ask

Is boost log thread safe?

It is thread-safe regardless of the kind of loggers you use.

What is boost logging?

Boost. Log provides the ability to filter log events based on the severity of the log record.

How do I open a ALOG file?

Because most log files are recorded in plain text, the use of any text editor will do just fine to open it. By default, Windows will use Notepad to open a LOG file when you double-click on it.


1 Answers

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.

like image 96
wonce Avatar answered Oct 26 '22 17:10

wonce