I'm introducing a replacement to the natively written logging framework of my application. The existing logging is written in a fashion to generate the files such that the file being currently written to is named "logs.txt" and the rolled over files are named as "Logs.N.txt", where "Logs.1.txt" is the latest after "logs.txt". How do I achieve the same behaviour through Boost V2 logging?
Trying to use the Boost logging because it provides good support for the multiple sinks, as I now have to target my logs to 3 locations: a) local log file, b) stack driver on cloud, and c) syslog server hosted as a separate container
The reason I want the current file to be "logs.txt" is that among other things, it allows one to just tail -F logs.txt
on a running system.
I found a snippet that rotates the logs and maintains the size limitations for both per-file & total-logs.
auto strm = boost::log::add_file_log(
boost::log::keywords::file_name = "Logs.%2N.txt",
boost::log::keywords::open_mode = std::ios_base::app,
boost::log::keywords::rotation_size = 5 * 1024, // Max filesize
boost::log::keywords::auto_flush = true
);
auto bkend = strm->locked_backend();
bkend->set_file_collector(boost::log::sinks::file::make_collector(
boost::log::keywords::target = "./", // log file destination
boost::log::keywords::max_size = 100 * 1024, //Max total size
boost::log::keywords::min_free_space = 100000
));
bkend->scan_for_files(boost::log::sinks::file::scan_method::scan_matching, true);
Behaviour
Current file generation pattern is:
Logs.01.txt <--- Oldest file
Logs.02.txt
.
.
.
Logs.19.txt
Logs.20.txt <--- File being written to
which as the logging continues will become
Logs.41.txt <--- Oldest file
Logs.42.txt
.
.
.
Logs.59.txt
Logs.60.txt <--- File being written to
The index just keeps rolling on (so it's beyond the desired 2 digit index)
Logs.131.txt <--- Oldest file
Logs.132.txt
.
.
.
Logs.149.txt
Logs.150.txt <--- File being written to
Required file generation pattern is:
logs.txt <--- File being written to
Logs.01.txt <--- Latest rolled over file
Logs.02.txt
.
.
.
Logs.12.txt
Logs.13.txt <--- Oldest file
grows to
logs.txt <--- File being written to
Logs.01.txt <--- Latest rolled over file
Logs.02.txt
.
.
.
Logs.19.txt
Logs.20.txt <--- Oldest file
& as Logs.20.txt
is at the limit of the total space, it keeps overwriting the Logs.20.txt
file with Logs.19.txt
and so on for each rollover.
So the oldest file keeps getting renamed to the next index, till it reaches the Max total log space limit and is then just overwritten.
Questions
- Is there a configuration for file logging backend that can support it?
- If not, how can I customise the backend for this?
- Also, please point me to any documentation/tutorial (other than Boost.Log documentation) to Boost logging that speaks to the library structure and class level interactions, if aware.