Boost日志文件旋转未按预期工作



我对使用boost库没有太多经验。我正在使用boost库来维护带有文件轮换的系统日志,但它并没有按预期工作。正在无休止地创建日志文件。当文件大小达到1MB时,将创建一个新的日志文件。目前,我只希望存在5个日志文件,每个文件的大小为1MB,必须添加新的日志来代替旧的日志。在任何情况下,为日志文件分配的内存都不得超过5 MB。请帮忙。我正在使用以下函数初始化日志旋转。

static void fnInitSystemLogging(string logfile, bool log_level)
{
static const std::string COMMON_FMT("[%TimeStamp%] [%Severity%]:t%Message%");
boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
// get current data and time and convert it into string
boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();
const std::string str_date = to_simple_string(timeLocal.date());
const std::string str_time = to_simple_string(timeLocal.time_of_day());
// Output message to console
boost::log::add_console_log(
std::cout,
boost::log::keywords::format = COMMON_FMT,
boost::log::keywords::auto_flush = true
);
// Output message to file, rotates when file reached 1mb or at midnight every day. Each log file
// is capped at 1mb and total is 5mb
boost::log::add_file_log (
boost::log::keywords::file_name = logfile + "_" +  str_date + "_" + str_time + "_%3N.log",
boost::log::keywords::rotation_size = 1 * 1024 * 1024,
boost::log::keywords::max_size = 5 * 1024 * 1024,
boost::log::keywords::max_files = 5,
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
boost::log::keywords::format = COMMON_FMT,
boost::log::keywords::auto_flush = true
);
boost::log::add_common_attributes();
// Only output message with INFO or higher severity in Release
// #ifndef _DEBUG
if(log_level)
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
else
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::error);
//#endif
}

保持文件总大小和数量的限制是文件收集器的一项功能。将为目标存储目录创建文件收集器,旋转后的文件将在旋转时移动到该目录。必须在keywords::target参数中指定目标存储目录,才能启用文件收集器和所有相关的限制。目标存储目录可以与当前写入的日志文件所在的目录相同。

您应该精确boost::log::keywords::max_files

boost::log::add_file_log (
boost::log::keywords::file_name = logfile + "_" +  str_date + "_" + str_time + "_%3N.log",
boost::log::keywords::rotation_size = 1 * 1024 * 1024,
boost::log::keywords::max_size = 5 * 1024 * 1024,
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
boost::log::keywords::format = COMMON_FMT,
boost::log::keywords::auto_flush = true,
boost::log::keywords::max_files = 5
);

最新更新