gpt4 book ai didi

c++ - 多文件后端和严重性

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:05 24 4
gpt4 key购买 nike

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

我做错了什么?

最佳答案

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

关于c++ - 多文件后端和严重性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com