I want adjust severity level without recompile, so I use init_from_stream to read severity level from file. Other options I don't need change runtime, so their setting as before is done by boost.log API.
The config file is:
[Core]
DisableLogging=false
Filter="%Severity% >= 0"
The code is:
std::ifstream file("log_setting.ini");
boost::log::init_from_stream(file);
boost::log::register_simple_formatter_factory< severity_level, char >("Severity");
// following is same as before(all setting is by calling API, and comment out severity settings)
boost::shared_ptr<boost::log::core> core = boost::log::core::get();
core->set_exception_handler(boost::log::make_exception_suppressor());
//add and set sinks
But I found the init by file seems exclusive with "traditional API setting", if I add init_from_stream
, there is nothing log out.
My requirement is use setting file to control some parts of parameters which I need change runtime but no all of them.
You need to register the formatter factory before you use the formatter parser (which is used when you parse the formatter from the settings file).
The init_from_stream
function is not exclusive with respect to the library features, but it is hardly suitable for runtime updates of logging settings. Whatever sinks it recognizes in the settings, it will add to the logging core, whether or not those sinks were added before.
You will have to implement your own mechanism for updating logging configuration. You can use the settings parser to read the settings file, but you'll have to keep track of the sinks you add to the core and to initialize the sinks from the settings container yourself.