Use the hiearchy channel for Boost.Log to filter severity and shell

I studied Boost.Log for a while, and now I think it's time to move my code base from log4cxx to Boost.Log. I believe that the design and implementation of Boost.Log will significantly improve the maintenance and use of the code. I know that the Boost.Log FAQ has a page that says

As for hierarchical registrars, there is no need for this function in this library. One of the main advantages provided by log4j is the definition of applications (sinks, from the point of view of this library) in which the log entry will be completed. This library achieves the same result by filtering.

I understand conceptual equivalence and am not trying to make Boost.Log in log4j / log4cxx. Rather, my question is: how can I use Boost.Log to get the same functionality that I am currently using in log4cxx? In particular, I want to set severity thresholds and receivers for specific nodes in a log source or channel hierarchy. For example, I have logging sources organized libA.moduleB.componentC.logDwith levels in a hierarchy separated by dots .. Using log4cxx, you can set a common threshold libAin INFO with a more specific logger libA.moduleB, having a threshold of DEBUG.

libA.threshold=INFO
libA.moduleB.threshold=DEBUG

Similarly, you can attach receivers to arbitrary nodes in the hierarchy.

, Boost.Log, / , . , , , Boost.Log , .

.

+1
1

Boost.Log(, ) (, ) , . , , , , . , . - , , . , . , , . .

, , A A.log, B - B.log, - .

BOOST_LOG_ATTRIBUTE_KEYWORD(a_severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string)

logging::add_file_log(
    keywords::file_name = "A.log",
    keywords::filter = a_channel == "A");

logging::add_file_log(
    keywords::file_name = "B.log",
    keywords::filter = a_channel == "B");

. . , .

typedef src::severity_channel_logger< severity_level, std::string > logger_type;

logger_type lg_a(keywords::channel = "A");
logger_type lg_b(keywords::channel = "B");

BOOST_LOG_SEV(lg_a, info) << "Hello, A.log!";
BOOST_LOG_SEV(lg_b, info) << "Hello, B.log!";

, - .

. -, . , "A" "A.bb" . -, , , , (, "A" "A.bb" ). , .

, . . , - ; .

, , true, false . , Tutorial, . phoenix::bind Boost.Phoenix.

bool my_filter(
    logging::value_ref< severity_level, tag::a_severity > const& level,
    logging::value_ref< std::string, tag::a_channel > const& channel,
    channel_hierarchy const& thresholds)
{
    // See if the log record has the severity level and the channel attributes
    if (!level || !channel)
       return false;

    std::string const& chan = channel.get();

    // Parse the channel string, look for it in the hierarchy
    // and find out the severity threshold for this channel
    severity_level threshold = thresholds.find(chan);

    return level.get() >= threshold;
}

, :

logging::add_file_log(
    keywords::file_name = "A.log",
    keywords::filter = phoenix::bind(&my_filter, a_severity.or_none(), a_channel.or_none(), hierarchy_A));

logging::add_file_log(
    keywords::file_name = "B.log",
    keywords::filter = phoenix::bind(&my_filter, a_severity.or_none(), a_channel.or_none(), hierarchy_B));

hierarchy_A hierarchy_B - , .

+9

Source: https://habr.com/ru/post/1653130/


All Articles