多文件后端和严重性

时间:2013-08-13 10:27:57

标签: c++ logging boost

我将boost日志配置为有多个日志文件,但我的严重性级别有问题。我无法在日志中打印出来。

enum severity_level
{
    error = 0,
    critical = 1,
    info = 2,
    warning = 3,
    debug = 4,
    trace = 5
};

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, boost::log::sources::logger_mt);
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level);

.....

std::ostream& operator<< (std::ostream& strm, severity_level level)
{
    static const char* severity_name[] = {
        "ERR",
        "CRI",
        "INF",
        "WAR",
        "DEB",
        "TRA"
    };

    if (static_cast< std::size_t >(level) < sizeof(severity_name) / sizeof(*severity_name))
        strm << severity_name[level];
    else
        strm << static_cast< int >(level);

    return strm;
}

void log_init()
{
    // Create a text file sink
    typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_multifile_backend > file_sink;
    boost::shared_ptr< file_sink > sink(new file_sink);

    // Set up how the file names will be generated
    sink->locked_backend()->set_file_name_composer(boost::log::sinks::file::as_file_name_composer(
    boost::log::expressions::stream << "logs/" << boost::log::expressions::attr< std::string >("logger_name") << ".log"));

    // Set the log record formatter
    sink->set_formatter
    (
    boost::log::expressions::format("%1%: [%2%] - %3%")
    % boost::log::expressions::attr<severity_level>("Severity")
    % boost::log::expressions::attr< boost::posix_time::ptime >("TimeStamp")
    % boost::log::expressions::smessage
    );

    // Add it to the core
    boost::log::core::get()->add_sink(sink);

    // Add some attributes
    boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
    boost::log::core::get()->add_global_attribute("RecordID", boost::log::attributes::counter< unsigned int >());
}

但是当我尝试打印日志时,我在日志文件中没有严重性。

BOOST_LOG_SEV(my_logger::get(), severity_level::info)<<"some text logged";
: [2013-Aug-13 11:07:18.280748] - some text logged

我做错了什么?

1 个答案:

答案 0 :(得分:1)

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, boost::log::sources::logger_mt);

应改为

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, boost::log::sources::severity_logger_mt<severity_level>);

PS:BOOST日志很糟糕。配置非常复杂(我还没有正确配置)。你知道易用的log lib吗?我需要将日志写入不同的文件,严重性级别和旋转大小。