Search code examples
c++boostboost-log

Boost Log severity levels versus syslog severity levels


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?


Solution

  • 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.