我使用boost 1.62。我以这种方式启动记录器:
logging::add_file_log
(
keywords::file_name = "myfile.log",
keywords::rotation_size = 1024,
keywords::format = expr::stream
<< "["
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
<< "] <" << expr::attr< severity_level >("Severity")
<< "> " << expr::message
);
logging::add_common_attributes();
对于上面的代码,旋转工作正常,但是当程序启动时,从开始创建日志文件。当我添加:
keywords::open_mode = (std::ios::out | std::ios::app)
到add_file_log
新日志会附加到现有日志文件中,但轮换不起作用。我需要一个旋转的日志文件。当文件已存在时,应附加新数据。如何解决?
答案 0 :(得分:0)
配置记录器时应使用scan_matching
。参见例如
#define BOOST_LOG_DYN_LINK 1
#include <boost/log/core.hpp>
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <string>
void initLogging()
{
boost::log::add_common_attributes();
auto core = boost::log::core::get();
core->add_global_attribute("UTCTimeStamp",boost::log::attributes::utc_clock());
auto x = boost::log::add_file_log(
boost::log::keywords::file_name = "Log_%3N.log",
boost::log::keywords::rotation_size = 1 * 1024, // 1k
boost::log::keywords::target = "Logs",
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
boost::log::keywords::max_size = 20 * 1024,
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(boost::gregorian::greg_day(31)),
boost::log::keywords::scan_method = boost::log::sinks::file::scan_matching,
boost::log::keywords::format = "%UTCTimeStamp% (%TimeStamp%) [%ThreadID%]: %Message%",
boost::log::keywords::auto_flush = true
);
//auto d = x->locked_backend()->scan_for_files();
}
int main()
{
initLogging();
for (int i = 0; i < 20; ++i) {
BOOST_LOG_TRIVIAL(trace) << "Let's go shopping " << std::string(400, '*');
}
}