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

C++ Boost日志文件旋转未按预期工作,c++,boost,C++,Boost,我没有太多使用boost库的经验。我使用boost库来维护文件循环的系统日志,但它并没有按预期工作。 正在无休止地创建日志文件。 当文件大小达到1 MB时,将创建一个新的日志文件。目前,我只希望存在5个日志文件,每个文件的大小为1MB,并且必须添加新日志来代替旧日志。在任何情况下,为日志文件分配的内存不得超过5 MB。请帮忙。我正在使用以下函数初始化日志旋转 static void fnInitSystemLogging(string logfile, bool log_level) {

我没有太多使用boost库的经验。我使用boost库来维护文件循环的系统日志,但它并没有按预期工作。 正在无休止地创建日志文件。 当文件大小达到1 MB时,将创建一个新的日志文件。目前,我只希望存在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
}
static void fnInitSystemLogging(字符串日志文件,bool日志级别)
{
静态常量std::string COMMON_FMT(“[%TimeStamp%][%Severity%]:\t%Message%”);
boost::log::register\u simple\u formatter\u factory(“severity”);
//获取当前数据和时间并将其转换为字符串
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());
//将消息输出到控制台
boost::log::添加控制台日志(
std::cout,
boost::log::keywords::format=COMMON\u FMT,
boost::log::keywords::auto_flush=true
);
//将消息输出到文件,当文件达到1mb或每天午夜时旋转。每个日志文件
//上限为1mb,总容量为5mb
boost::log::添加文件\u日志(
boost::log::keywords::file\u name=logfile+“\u”+str\u date+“\u”+str\u time+“\u3n.log”,
boost::log::keywords::rotation\u size=1*1024*1024,
boost::log::keywords::max_size=5*1024*1024,
boost::log::keywords::max_files=5,
boost::log::keywords::time\u-based\u-rotation=boost::log::sinks::file::rotation\u在时间点(0,0,0),
boost::log::keywords::format=COMMON\u FMT,
boost::log::keywords::auto_flush=true
);
boost::log::添加公共属性();
//仅在版本中输出具有INFO或更高严重性的消息
//#ifndef#u调试
如果(日志级别)
boost::log::core::get()->设置过滤器(boost::log::Trial::severity>=boost::log::Trial::info);
其他的
boost::log::core::get()->设置过滤器(boost::log::Trial::severity>=boost::log::Trial::error);
//#恩迪夫
}

您应该精确
boost::log::keywords::max\u文件

boost::log::添加文件\u日志(
boost::log::keywords::file\u name=logfile+“\u”+str\u date+“\u”+str\u time+“\u3n.log”,
boost::log::keywords::rotation\u size=1*1024*1024,
boost::log::keywords::max_size=5*1024*1024,
boost::log::keywords::time\u-based\u-rotation=boost::log::sinks::file::rotation\u在时间点(0,0,0),
boost::log::keywords::format=COMMON\u FMT,
boost::log::keywords::auto_flush=true,
boost::log::keywords::max_files=5
);

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

请告诉我们您的期望和当前拥有的内容。“无限创建”是什么意思?预期行为:总共生成5个日志文件(test1.log、test2.log、…、test5.log),每个文件的大小为1MB。当日志大小超过5 MB而不是创建新日志文件(test6.log)时,系统应删除旧日志并将日志添加到现有日志文件中(例如test5.log,假设最新日志始终附加在test1.log中)。当前行为:当日志大小超过5 MB时,将创建新日志文件(test6.log)。当达到1MB大小时,将创建一个新的日志文件,从而无休止地创建日志文件。@Hizza您能编辑您的问题使其更清晰吗?现在这个评论很难阅读,看起来它重复了大部分问题。也许把它放到问题中,并将其格式化为一个位添加关键字“max_files”没有任何效果。正在经历相同的行为。确实要重新编译吗?这个程序有多个并行运行的实例吗?运行此代码的多个线程?还请注意,这些日志旋转约束仅适用于当前运行的实例。每次你运行你的程序,它都会从头开始创建一个新的文件,并遵守所有这些约束条件。此外,我还意识到每次运行都会创建自己的文件的限制。同一个实例会创建5个以上具有这些限制的文件?+1我想我已经看到这个问题被问了几十次了,这是我第一次看到我记得的有针对性的答案。我们有没有办法改进文档以防止再次出现这种意外?感谢Andrey的帮助,包括target关键字解决了我的问题。指定目标目录启用了文件轮换。@sehe我在
添加文件日志
描述中添加了一个注释,尽管我似乎很清楚,为了保持目标目录限制,必须首先指定它。感谢@Andreyesmashev-有时经验证据是最好的指南。我同意这一点——如果我从现在开始的话,这已经很清楚了。我猜很多人是从样品中取而代之的。我很想对关键字进行分组(比如
collector::max_size
),但既然已经没有了,我想我们可以通过一些冗余提示左右引导人们。