0

I'm trying to modify my application to use the Boost Log library instead of logging into syslog. Boost Log severity levels are defined in boost/log/trivial.hpp, where the most serious level has maximal numeric number (5):

enum severity_level
{
    trace,
    debug,
    info,
    warning,
    error,
    fatal
};

However, the syslog defines more severity levels, which are actually standardized by the RFC5424 - and most serious level has minimal numeric number (0).

Is it any way to define my own MySeverityLevels enumeration type (probably close to the RFC5424) and use various Boost Log loggers (severity_logger, for instance) with this new type, including the filtering by severity level?

HEKTO
  • 3,876
  • 2
  • 24
  • 45

2 Answers2

1

<boost_root>/libs/log/example/native_syslog contains example of custom log levels, i.e. create an enumeration for your severity levels:

//! Define application-specific severity levels
enum severity_levels
{
    /* your own level */
    normal = 2,
    warning = 1,
    error = 0
};

and register it in your sink

    // Create a syslog sink
    shared_ptr< sinks::synchronous_sink< sinks::syslog_backend > > sink(
        new sinks::synchronous_sink< sinks::syslog_backend >(
            keywords::use_impl = sinks::syslog::native,
            keywords::facility = sinks::syslog::local7));
     ........................
    // We'll have to map our custom levels to the syslog levels
    sinks::syslog::custom_severity_mapping< severity_levels > mapping("Severity");
    mapping[normal] = sinks::syslog::info;
    mapping[warning] = sinks::syslog::warning;
    mapping[error] = sinks::syslog::critical;

    sink->locked_backend()->set_severity_mapper(mapping);

    // Add the sink to the core
    logging::core::get()->add_sink(sink);
Victor Gubin
  • 2,782
  • 10
  • 24
1

I'm answering my own question. The code below implements what I wanted:

#include <boost/log/common.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

enum class MySeverityLevel
{
  panic,
  alert,
  critical,
  error,
  warning,
  notice,
  info,
  debug
};

BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "Severity", MySeverityLevel)

int main()
{
  // ------ define sink
  logging::add_file_log
  (
    keywords::file_name = "test.log",
    keywords::filter = (Severity <= MySeverityLevel::error)
  );
  // ------ define logger
  src::severity_logger<MySeverityLevel> lg;
  // ------ output logging messages
  BOOST_LOG_SEV(lg, MySeverityLevel::panic) << "This is panic";
  BOOST_LOG_SEV(lg, MySeverityLevel::debug) << "This is debug";
}

It was no need to use any mappings.

HEKTO
  • 3,876
  • 2
  • 24
  • 45