Search code examples
c++boostboost-log

boost logging severity level filter not honored


This is driving me nuts. I am setting the severity level explicitly but boost.log does not honour it. All messages with all severity levels are logged.

here is the code I am using:

the is_verbose flag obviously obtained from command line. And a message logged via:

BOOST_LOG_TRIVIAL(debug) << "Hello there!" ;

will be logged and shown on terminal.

Thanks.

#include "boost/log/core.hpp"
#include "boost/log/expressions.hpp"
#include "boost/log/utility/setup/file.hpp"
#include "boost/log/trivial.hpp"
#include "boost/log/attributes.hpp"
#include "boost/log/utility/setup/common_attributes.hpp"

#include "Logger.h"

namespace logging = boost::log;

void Logger::init(bool is_verbose) {

    if(is_verbose)
        logging::core::get()->set_filter(
            logging::trivial::severity >= logging::trivial::info);
    else
        logging::core::get()->set_filter(
            logging::trivial::severity >= logging::trivial::error);   

    namespace keywords = boost::log::keywords;
    namespace sinks = boost::log::sinks;

    logging::add_file_log(
            keywords::auto_flush = true,
            keywords::file_name = "sample_%N.log",
            keywords::rotation_size = 10 * 1024 * 1024,
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
            keywords::format = "[%TimeStamp%][%Severity%]: %Message%"
            );

    logging::add_common_attributes();

}

Solution

  • This works

    Live On Coliru

    #define BOOST_ALL_DYN_LINK
    
    #include <boost/log/core.hpp>
    #include <boost/log/sources/logger.hpp>
    #include <boost/log/expressions.hpp>
    #include <boost/log/utility/setup/file.hpp>
    #include <boost/log/trivial.hpp>
    #include <boost/log/attributes.hpp>
    #include <boost/log/utility/setup/common_attributes.hpp>
    
    namespace logging = boost::log;
    
    struct Logger {
        void init(bool is_verbose) {
            namespace keywords = boost::log::keywords;
            namespace sinks    = boost::log::sinks;
            namespace trivial  = boost::log::trivial;
    
            if(!is_verbose)
                logging::core::get()->set_filter(trivial::severity >= trivial::warning);   
    
            logging::add_file_log(
                    keywords::auto_flush          = true,
                    keywords::file_name           = is_verbose? "verbose_%N.log":"sample_%N.log",
                    keywords::rotation_size       = 10 * 1024 * 1024,
                    keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
                    keywords::format              = "[%TimeStamp%][%Severity%]: %Message%"
                );
    
            logging::add_common_attributes();
        }
    };
    
    int main(int argc, char**) {
        Logger logger;
        logger.init(argc>1);
    
        {
            using sl = boost::log::trivial::severity_level;
            logging::sources::severity_logger<sl> lg;
    
            BOOST_LOG_SEV(lg, sl::trace) << "trace";
            BOOST_LOG_SEV(lg, sl::fatal) << "fatal";
        }
    
        BOOST_LOG_TRIVIAL(trace) << "trivial trace";
        BOOST_LOG_TRIVIAL(fatal) << "trivial fatal";
    }
    

    Testing:

    g++ -std=c++11 -Os -Wall -pedantic -pthread main.cpp \
          -lboost_thread -lboost_system -lboost_log -lboost_log_setup
    ./a.out
    ./a.out -v
    tail *.log
    

    Prints

    ==> sample_0.log <==
    [2015-Sep-02 09:49:22.172371][]: fatal
    [2015-Sep-02 09:49:22.174915][]: trivial fatal
    
    ==> verbose_0.log <==
    [2015-Sep-02 09:49:22.226536][]: trace
    [2015-Sep-02 09:49:22.227292][]: fatal
    [2015-Sep-02 09:49:22.227380][]: trivial trace
    [2015-Sep-02 09:49:22.227408][]: trivial fatal