问题描述
我最近尝试通过Boost日志向一些小型应用程序添加一些日志记录。但是,使用日志循环,由于某种原因,我无法使其在日志目录中选择正确的计数器。
例如,如果我的日志
目录包含文件 Log_000.log
和 Log_001.log
以 Log_002.log
开头的日志记录,但始终总是从0开始。
我可能丢失了一些内容但如果有人能发现我将不胜感激。这是我的启动代码:
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 = 2 * 1024,// 2k
boost :: log :: keywords :: target = Logs,
boost :: log :: keywords :: min_free_space = 30 * 1024 * 1024,
boost :: log :: keywords :: max_size = 20 * 1024,// 20k
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();
}
非常感谢
啊!看起来,如果您使用 add_file_log
助手,它将自动调用 scan_for_files
。
再次调用它似乎会使计数器重置,所以我只是删除了对 scan_for_files
$的调用p>
只是弹出一个答案,以防万一它使其他人发疯!
这是一个可行的示例
运行10次后,您会看到 Logs /
包含:
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_080.log
-rw-r --r-- 1 2001 2000 998 Jul 17 16:26 Log_081.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_082.log
-rw-r --r-- 1 2001 2000 998 Jul 17 16:26 Log_083.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_084.log
-rw-r --r-- 1 2001 2000 998 Jul 17 16:26 Log_085.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_086.log
-rw-r --r-- 1 2001 2000 998 Jul 17 16:26 Log_087.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_088.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_089.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_090.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_091.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_092.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_093.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_094.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_095.log
-rw-r--r-- 1 2001 2000 998七月17:26 Log_096.log
-rw-r--r-- 1 2001 2000 998七月17:26 Log_097.log
-rw-r--r-- 1 2001 2000 998七月17 16:26 Log_098.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_099.log
代码清单
#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
);
//自动d = x-> locked_backend()-> scan_for_files();
}
int main()
{
initLogging();
for(int i = 0; i< 20; ++ i){
BOOST_LOG_TRIVIAL(trace)<< 我们一起去购物<< std :: string(400,‘*’);
}
}
I've recently tried to add some logging via Boost log to a small application. However, using log rotation I can't for some reason get it to pick up the correct counter in the logs directory.
For example, if my Logs
directory contains the files Log_000.log
and Log_001.log
I'd expect the logging to start with Log_002.log
but it always starts again from 0.
I'm probably missing something but if anyone can spot something I'd be grateful. Here's my startup code:
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 = 2 * 1024, // 2k
boost::log::keywords::target = "Logs",
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
boost::log::keywords::max_size = 20 * 1024, // 20k
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();
}
Many thanks
Aha! It looks like if you use the add_file_log
helper it automatically calls scan_for_files
.
Calling it again causes the counter to be reset it seems, so I just removed my call to scan_for_files
Just popped this on as an answer in case it was driving anyone else mad!
Here's a working sample
You'll see that after it ran 10 times, Logs/
contains:
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_080.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_081.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_082.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_083.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_084.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_085.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_086.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_087.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_088.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_089.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_090.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_091.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_092.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_093.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_094.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_095.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_096.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_097.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_098.log
-rw-r--r-- 1 2001 2000 998 Jul 17 16:26 Log_099.log
Code Listing
#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, '*');
}
}
这篇关于Boost :: Log和日志编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!