I'm using boost::log to simultaneously log to a file as well as the console. It's initialized like so:
void Init() {
logging::core::get()->set_filter
(
// set logging level to one of trace, debug, info, warning, error, fatal
logging::trivial::severity >= logging::trivial::trace
);
logging::add_common_attributes(); // enables timestamps and such
logging::add_file_log
(
keywords::file_name = logfile + "_%N.log",
keywords::rotation_size = 1024 * 1024 * 50, // 50MB log file max
// keywords::format = "[%TimeStamp%]: %Message%"
keywords::format = "%Message% #[%TimeStamp%]"
);
logging::add_console_log(std::cout,
keywords::format = "%Message%"
);
}
At certain points in my program I'd like to change the log file manually. I can change the "logfile" string in the above code and call that Init() again, but then it continues to write to the old log file and starts the new one, and starts doubling the output on the console log.
Is there some kind of corresponding "remove_file_log" I'm missing, or manually tell it to stop logging to the original log and move to the next one?
Every call to
add_file_log
registers a new file sink in the logging core, which results in multiple files being written at the same time. The existing sinks are not replaced. Depending on what you actually want to do, there are multiple things you could do.First, if you look at
add_file_log
reference, you will notice that it returns the pointer to the created sink. You can pass that pointer tocore::remove_sink
when you want that sink to stop being used. Upon being destroyed, the sink will close the log file it was using.Second, the
text_file_backend
which is created and returned byadd_file_log
has the ability to rotate log files. If you just want to switch the log file at certain points of the program, this is what you're probably looking for. You can add the named parameters such askeywords::rotation_size
to theadd_file_log
call and they will be used to set up automatic file rotation in the backend. You can also rotate the file manually by callingtext_file_backend::rotate_file
on the sink backend.Note that in this case you don't have to remove the sink from the core; it stays active after the file has been rotated.