Boost::日志和日志编号
Boost::Log and log numbering
我最近尝试通过Boost日志向一个小应用程序添加一些日志记录。但是,由于某些原因,使用日志旋转我不能让它在logs目录中拾取正确的计数器。
例如,如果我的Logs
目录包含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
的调用
只是把这个作为一个答案,以防它把别人逼疯了!
这是一个工作示例
Live On Coliru
您将看到运行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 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
代码清单
#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, '*');
}
}
相关文章:
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 是否可以配置提升日志刷新?
- 如何在数组中找到最小编号的索引号?
- 跟踪日志中的T.11803()是什么意思?
- 贪婪算法编号列表
- 加快在C++中读取/处理日志文件的速度
- 如何将消息时间戳写入日志文件?
- 分析包含 NMEA 句子的日志文件C++
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- 为什么在为其分配编号后无法显示我的字符串值?
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- Log4net,将日志消息从 c++ dll 发送到 c# 应用程序?
- 返回ERROR_INVALID_PARAMETER的事件日志函数
- 为什么系统日志有两个不同的函数声明?
- 从更改日志获取最新的 USN 数据
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 输出返回编号。等于输入字符串的单词的字符串数
- C++ gRPC 线程编号配置
- Boost::日志和日志编号