提升日志记录严重性级别过滤器不受尊重

时间:2015-09-02 06:52:58

标签: c++ boost boost-log

这让我疯了。我明确设置了严重性级别,但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();

}

1 个答案:

答案 0 :(得分:1)

这有效

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