Menu

Separate severity level filter per channel

aleethorp
2012-10-08
2013-06-03
  • aleethorp

    aleethorp - 2012-10-08

    Hi I'd like to achieve the equivalent of having multiple channel_severity loggers each with their own filter.

    So for example,

    typedef severity_channel_logger<severity_levels, std::string> my_logger;
    my_logger lg1(keywords::channel = "subsystem1");
    my_logger lg2(keywords::channel = "subsystem2");
    BOOST_LOG_SEV(lg1, normal) << "Hello, world";
    BOOST_LOG_SEV(lg2, normal) << "Hello, world";
    

    In fact the above channels (names) could be created on the fly. I do not know a-priori what the channels will be so I can use the usual filter examples:

    sink->set_filter
    (
        flt::attr< int >("Severity") >= 5 &&
        flt::attr< std::string >("Channel") == "net"
    );
    

    Instead I need to support something else that logically looks like the following

    sink->set_filter
    (
        // Not valid boost.log filter
       ((has_channel_attribute() ? is_channel_logged(log_record.channel_name(), log_record.severity()) : true) &&
       // other filters
    );
    

    My function is_channel_logged() needs to take the channel name as an argument and look elsewhere (e.g. look up in the application configuration) whether this channel meets the severity level requirements for being logged.

    Looking at the documentation it looks like satisfies is kind of what I need, however, I need to take 2 arguments: channel_name and severity_level in my user-defined predicate.

    bool check_severity_level(int level);
    sink->set_filter
    (
        flt::attr< int >("Severity").satisfies(&check_severity_level)
    );
    

    This doesn't seem to quite fit the original use case for channels but it is the closest thing I have.

     
  • Andrey Semashev

    Andrey Semashev - 2012-10-08

    Currently you have to write your own filter. The filter accepts a view of all attribute values that are going to be attached to the log record; the view will include both the channel name and severity level. I don't have an example at hand now but you can take a look at library-provided filters (e.g. has_attr) for guidance.

     
  • aleethorp

    aleethorp - 2012-10-10

    Thanks, I will give it a try and drop a not as to how it went.

     

Log in to post a comment.