1

我将 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

我做错了什么?

4

1 回答 1

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日志很烂。配置非常复杂(我还没有正确配置)。你知道易于使用的日志库吗?我需要将日志写入不同的文件、严重级别和轮换大小。

于 2013-08-14T08:04:41.960 回答