2

I am using Boost.Log 1.55.0 in a project and I do want to change the severity filter for all sinks of a boost::log::sources::severity_logger instance.

Here's an example how-to setup one sink with an initial severity filter:

void InitializeLogging(LogLevels const kLogLevel) const {
    auto line_id = boost::log::expressions::attr<unsigned int>("LineID");
    auto severity = boost::log::expressions::attr<LogLevels>("Severity");
    auto timestamp = boost::log::expressions::format_date_time<boost::posix_time::ptime>(
        "TimeStamp",
        "%Y-%m-%d %H:%M:%S");

    boost::log::formatter const kFormatter{
        boost::log::expressions::stream
        << std::setw(6) << std::setfill('0') << line_id
        << std::setfill(' ')
        << ": " << timestamp
        << " [" << severity << "] "
        << boost::log::expressions::smessage};

    using TextSink = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;
    boost::shared_ptr<TextSink> sink = boost::make_shared<TextSink>();
    boost::shared_ptr<std::ostream> stream(&std::clog, boost::empty_deleter());
    sink->locked_backend()->add_stream(stream);
    sink->set_filter(severity >= kLogLevel);
    sink->set_formatter(kFormatter);

    boost::log::core::get()->add_sink(sink);
    boost::log::add_common_attributes();
}

So I am able to setup the filtering when I create the sink via the member function set_filter, but I want to know how-to modify the filter of one/more/all sinks that are configured for the core of Boost.Log.

  1. Is there any function I did not see yet to modify existing sinks?
  2. If not, do I have to remove the sinks from the core and "re-create" them?
Florian Wolters
  • 3,820
  • 5
  • 35
  • 55

2 Answers2

1

I found out the solution by myself. Therefore I will answer my two questions here:

  1. Such a function does not exist, or I am unable to find it.
  2. No, it is possible to set the filter for all sinks by calling the function set_filter on the core of Boost.Log. I am using the following function for my custom severity log levels:

    void log_level(LogLevels const kLogLevel) {
      boost::log::core::get()->set_filter(boost::log::expressions::attr<CustomLogLevels>("Severity") >= kLogLevel);
    }
    

Sidenote: Removing all sinks from the core is possible by calling boost::log::core::get()->remove_all_sinks(), but not necessary in my use case.

Florian Wolters
  • 3,820
  • 5
  • 35
  • 55
  • Hey, Florian, I'm having trouble filtering the severity. My question is here: http://stackoverflow.com/q/29707017/1735836 – Patricia Apr 17 '15 at 18:54
0

I found the following to be a simple way to change the filter:

Given the following setup performed during log initialization:

boost::shared_ptr<file_sink> mSink;
mSink(new file_sink(keywords::file_name = "BoostLog_%Y%m%d_%3N.txt", keywords::rotation_size = 500 * 1024))
mSink->set_filter(severity >= error);

I logged lots of messages with the original filter, then I changed the filter to a new severity level with the following:

mSink->reset_filter();
mSink->set_filter(severity >= warning);

The logging then payed attention to the new filter setting.

f1fan44
  • 141
  • 1
  • 9