如何通过配置文件配置Boost Log的TextFile Sink以追加模式写入日志?
Great question! Boost Log 1.57's default configuration system doesn't directly expose the open_mode setting for TextFile sinks in the config file (which is why you didn't see it in that docs table). But you can absolutely add this functionality—here are two solid approaches:
1. Extend the Config System with Custom Settings (Recommended for 1.57)
You can register a custom sink factory to handle an OpenMode config parameter, then map it to the std::ios_base::app flag. Here's how to implement it:
Step 1: Add Custom Setup Code
First, write code to parse the open mode string from the config and apply it to the text file backend:
#include <boost/log/core.hpp> #include <boost/log/expressions.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include <boost/log/utility/setup/file.hpp> #include <boost/log/utility/setup/settings.hpp> #include <boost/log/utility/setup/from_settings.hpp> namespace logging = boost::log; namespace sinks = logging::sinks; // Parse open mode string (supports "app" for append; extend as needed) std::ios_base::openmode parse_open_mode(const std::string& mode_str) { // Default to Boost's default: truncate existing file std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc; if (mode_str.find("app") != std::string::npos) { // Replace truncate with append mode &= ~std::ios_base::trunc; mode |= std::ios_base::app; } // Add support for other modes like "ate" or "binary" if you need them return mode; } // Register a custom factory for TextFile sinks to handle OpenMode void register_custom_text_file_sink() { auto sink_factory = logging::register_sink_factory("TextFile", [] (const logging::settings& settings) -> logging::sink_ptr { // Create the default text file backend auto backend = boost::make_shared<sinks::text_file_backend>(); // Let Boost handle its default config parameters first (FileName, RotationSize, etc.) logging::setup::parse_text_file_settings(settings, *backend); // Check for our custom OpenMode parameter if (settings.has("OpenMode")) { std::string mode = settings["OpenMode"].get<std::string>(); backend->set_open_mode(parse_open_mode(mode)); } // Wrap the backend in a synchronous sink and return return boost::make_shared<sinks::synchronous_sink<sinks::text_file_backend>>(backend); }); } int main() { // Register the custom factory BEFORE loading config register_custom_text_file_sink(); // Load your log config file logging::settings log_config = logging::parse_settings("your_log_config.conf"); logging::init_from_settings(log_config); // Test the append behavior BOOST_LOG_TRIVIAL(info) << "This line will append to the log file on restart!"; return 0; }
Step 2: Update Your Config File
Now you can add the OpenMode parameter to your TextFile sink section:
[Sinks.MyFileSink] Destination=TextFile FileName=application_%Y%m%d.log RotationSize=10485760 ; 10MB per file OpenMode=app ; Enable append mode
2. Upgrade Boost Log (Simpler, If Possible)
If you can upgrade to Boost Log 1.60 or later, the OpenMode parameter is natively supported in the config file! You don't need any custom code—just add the same OpenMode=app line to your sink config, and it will work out of the box.
Either way, you’ll get the append behavior you want when restarting your process.
内容的提问来源于stack exchange,提问作者Karthik K N

