提升日志记录严重性级别过滤器未兑现

boost logging severity level filter not honored

这让我抓狂。我正在明确设置严重性级别,但 boost.log 不遵守它。记录具有所有严重级别的所有消息。

这是我使用的代码:

is_verbose 标志显然是从命令行获得的。并通过以下方式记录了一条消息:

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

将被记录并显示在终端上。

谢谢。

#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();

}

这个有效

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";
}

测试:

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

版画

==> 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