[C++]Boost log 限制日志文件大小 和 数量

使用过 python 日志模块, 滚动设置很好用, 能限制日志文件数量 , 不至于长时间运行爆掉硬盘 .
网上找了一圈, 也没找到想要的结果, 有人还自己实现了限制文件数量…太麻烦了
最后自己试出来了, 分享一下:

#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/detail/format.hpp>
#include <boost/log/detail/thread_id.hpp>
#include <boost/algorithm/string.hpp>   


namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace trvl = boost::log::trivial;

MyLog::MyLog()
{
    _fileCount = 4;
    _fileSize = 1;
    _logLevel = boost::log::trivial::severity_level::info;
    FMT = "%Y-%m-%d %H:%M:%S";
}

MyLog::~MyLog()
{

}

void MyLog::init(const std::string& filename)
{
    _filename = filename;
    boost::filesystem::path filepath = boost::filesystem::complete(_filename);
    boost::filesystem::path dir = filepath.parent_path();
    if (boost::filesystem::exists(dir) == false)
    {
        boost::filesystem::create_directories(dir);
    }
    auto fmt =
        (
            expr::stream
            << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", FMT)
            << " [" << boost::log::trivial::severity
            << "] " << expr::smessage
            );
    auto consoleSink = boost::log::add_console_log
    (   std::cout,
        keywords::filter = expr::attr< trvl::severity_level >("Severity") >= (trvl::severity_level)_logLevel,
        keywords::auto_flush = true,
        keywords::format = fmt
    );

    auto sink = boost::log::add_file_log
    (
        keywords::open_mode = std::ios::app, 
        keywords::file_name = filename,     // 写入的日志文件名, 如 log.log
        keywords::target_file_name = "log_%Y%m%d-%N.log",  // 备份日志文件命名格式, 必须
        keywords::rotation_size = _fileSize * 1024 * 100,  // 日志文件大小 100KB
        keywords::filter = expr::attr< trvl::severity_level >("Severity") >= (trvl::severity_level)_logLevel,
        keywords::auto_flush = true,
        keywords::format = fmt
    );
    sink->locked_backend()->set_file_collector(sinks::file::make_collector(
        keywords::target = "",                         // 备份日志文件保存目录
        keywords::max_size = _fileSize * _fileCount * 1024 * 100  //所有日志加起来的最大大小
    ));
    boost::log::add_common_attributes();
}

我限制了文件数量 4 个, 结果与设置一致, 算上 log.log 始终是四个文件
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值