当前位置: 首页 > news >正文

聊聊logback的FixedWindowRollingPolicy

本文主要研究一下logback的FixedWindowRollingPolicy

RollingPolicy

ch/qos/logback/core/rolling/RollingPolicy.java

/*** A <code>RollingPolicy</code> is responsible for performing the rolling over* of the active log file. The <code>RollingPolicy</code> is also responsible* for providing the <em>active log file</em>, that is the live file where* logging output will be directed.* * @author Ceki G&uuml;lc&uuml;*/
public interface RollingPolicy extends LifeCycle {/*** Rolls over log files according to implementation policy.* * <p>* This method is invoked by {@link RollingFileAppender}, usually at the behest* of its {@link TriggeringPolicy}.* * @throws RolloverFailure Thrown if the rollover operation fails for any*                         reason.*/void rollover() throws RolloverFailure;/*** Get the name of the active log file.* * <p>* With implementations such as {@link TimeBasedRollingPolicy}, this method* returns a new file name, where the actual output will be sent.* * <p>* On other implementations, this method might return the FileAppender's file* property.*/String getActiveFileName();/*** The compression mode for this policy.* * @return*/CompressionMode getCompressionMode();/*** This method allows RollingPolicy implementations to be aware of their* containing appender.* * @param appender*/void setParent(FileAppender<?> appender);
}

RollingPolicy接口定义了rollover、getActiveFileName、getCompressionMode、setParent方法

RollingPolicyBase

ch/qos/logback/core/rolling/RollingPolicyBase.java

/*** Implements methods common to most, it not all, rolling policies. Currently* such methods are limited to a compression mode getter/setter.* * @author Ceki G&uuml;lc&uuml;*/
public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {protected CompressionMode compressionMode = CompressionMode.NONE;FileNamePattern fileNamePattern;// fileNamePatternStr is always slashified, see setterprotected String fileNamePatternStr;private FileAppender<?> parent;// use to name files within zip file, i.e. the zipEntryFileNamePattern zipEntryFileNamePattern;private boolean started;/*** Given the FileNamePattern string, this method determines the compression mode* depending on last letters of the fileNamePatternStr. Patterns ending with .gz* imply GZIP compression, endings with '.zip' imply ZIP compression. Otherwise* and by default, there is no compression.* */protected void determineCompressionMode() {if (fileNamePatternStr.endsWith(".gz")) {addInfo("Will use gz compression");compressionMode = CompressionMode.GZ;} else if (fileNamePatternStr.endsWith(".zip")) {addInfo("Will use zip compression");compressionMode = CompressionMode.ZIP;} else {addInfo("No compression will be used");compressionMode = CompressionMode.NONE;}}//......
}    

RollingPolicyBase定义了compressionMode、fileNamePattern、fileNamePatternStr、parent、zipEntryFileNamePattern、started;
determineCompressionMode方法会根据fileNamePatternStr的后缀来判断,默认支持gz、zip

FixedWindowRollingPolicy

ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java

public class FixedWindowRollingPolicy extends RollingPolicyBase {static final String FNP_NOT_SET = "The \"FileNamePattern\" property must be set before using FixedWindowRollingPolicy. ";static final String PRUDENT_MODE_UNSUPPORTED = "See also " + CODES_URL + "#tbr_fnp_prudent_unsupported";static final String SEE_PARENT_FN_NOT_SET = "Please refer to " + CODES_URL + "#fwrp_parentFileName_not_set";int maxIndex;int minIndex;RenameUtil util = new RenameUtil();Compressor compressor;public static final String ZIP_ENTRY_DATE_PATTERN = "yyyy-MM-dd_HHmm";/*** It's almost always a bad idea to have a large window size, say over 20.*/private static int MAX_WINDOW_SIZE = 20;public FixedWindowRollingPolicy() {minIndex = 1;maxIndex = 7;}//......
}    

FixedWindowRollingPolicy继承了RollingPolicyBase,他定义了minIndex、maxIndex、compressor属性

start

    public void start() {util.setContext(this.context);if (fileNamePatternStr != null) {fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);determineCompressionMode();} else {addError(FNP_NOT_SET);addError(CoreConstants.SEE_FNP_NOT_SET);throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET);}if (isParentPrudent()) {addError("Prudent mode is not supported with FixedWindowRollingPolicy.");addError(PRUDENT_MODE_UNSUPPORTED);throw new IllegalStateException("Prudent mode is not supported.");}if (getParentsRawFileProperty() == null) {addError("The File name property must be set before using this rolling policy.");addError(SEE_PARENT_FN_NOT_SET);throw new IllegalStateException("The \"File\" option must be set.");}if (maxIndex < minIndex) {addWarn("MaxIndex (" + maxIndex + ") cannot be smaller than MinIndex (" + minIndex + ").");addWarn("Setting maxIndex to equal minIndex.");maxIndex = minIndex;}final int maxWindowSize = getMaxWindowSize();if ((maxIndex - minIndex) > maxWindowSize) {addWarn("Large window sizes are not allowed.");maxIndex = minIndex + maxWindowSize;addWarn("MaxIndex reduced to " + maxIndex);}IntegerTokenConverter itc = fileNamePattern.getIntegerTokenConverter();if (itc == null) {throw new IllegalStateException("FileNamePattern [" + fileNamePattern.getPattern() + "] does not contain a valid IntegerToken");}if (compressionMode == CompressionMode.ZIP) {String zipEntryFileNamePatternStr = transformFileNamePatternFromInt2Date(fileNamePatternStr);zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context);}compressor = new Compressor(compressionMode);compressor.setContext(this.context);super.start();}

start方法先根据fileNamePattern来创建FileNamePattern,然后判断压缩模式,然后校验minIndex及maxIndex,要求相差不能超过MAX_WINDOW_SIZE(默认值为20),之后判断如果是zip模式的则创建zipEntryFileNamePattern,最后根据压缩模式创建compressor

rollover

    public void rollover() throws RolloverFailure {// Inside this method it is guaranteed that the hereto active log file is// closed.// If maxIndex <= 0, then there is no file renaming to be done.if (maxIndex >= 0) {// Delete the oldest file, to keep Windows happy.File file = new File(fileNamePattern.convertInt(maxIndex));if (file.exists()) {file.delete();}// Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1}for (int i = maxIndex - 1; i >= minIndex; i--) {String toRenameStr = fileNamePattern.convertInt(i);File toRename = new File(toRenameStr);// no point in trying to rename a nonexistent fileif (toRename.exists()) {util.rename(toRenameStr, fileNamePattern.convertInt(i + 1));} else {addInfo("Skipping roll-over for inexistent file " + toRenameStr);}}// move active file name to minswitch (compressionMode) {case NONE:util.rename(getActiveFileName(), fileNamePattern.convertInt(minIndex));break;case GZ:compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex), null);break;case ZIP:compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex),zipEntryFileNamePattern.convert(new Date()));break;}}}

rollover方法从maxIndex-1开始到minIndex,把这些文件名的序号加1,之后根据压缩模式判断,如果不压缩则把当前文件名重名为minIndex,若是gz压缩则把当前文件压缩然后命名为minIndex,若是zip压缩则把当前文件压缩然后命名为minIndex加上日期

小结

logback的FixedWindowRollingPolicy继承了RollingPolicyBase,实现了RollingPolicy接口,该接口定义了rollover、getActiveFileName、getCompressionMode、setParent方法,其中FixedWindowRollingPolicy的rollover的实现是根据minIndex及maxIndex来的,要求maxIndex及minIndex相差不能超过20,rollover的时候从maxIndex-1开始到minIndex,把这些文件名的序号加1,然后当前文件重命名为minIndex,其中还配合压缩模式进行压缩处理。

相关文章:

聊聊logback的FixedWindowRollingPolicy

序 本文主要研究一下logback的FixedWindowRollingPolicy RollingPolicy ch/qos/logback/core/rolling/RollingPolicy.java /*** A <code>RollingPolicy</code> is responsible for performing the rolling over* of the active log file. The <code>Roll…...

详解机器学习最优化算法

前言 对于几乎所有机器学习算法&#xff0c;无论是有监督学习、无监督学习&#xff0c;还是强化学习&#xff0c;最后一般都归结为求解最优化问题。因此&#xff0c;最优化方法在机器学习算法的推导与实现中占据中心地位。在这篇文章中&#xff0c;小编将对机器学习中所使用的…...

文件缓存的读写

文件系统的读写&#xff0c;其实就是调用系统函数 read 和 write。下面的代码就是 read 和 write 的系统调用&#xff0c;在内核里面的定义。 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) {struct fd f fdget_pos(fd); ......loff_t pos f…...

Debian 修改主机名

Debian 修改主机名 查看操作系统的版本信息设置主机名查看当前的主机名修改命令行提示符的格式 查看操作系统的版本信息 # cat /etc/issue Debian GNU/Linux 11 \n \l# lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 1…...

多线程返回计时问题代码案例

Component Slf4j Async public class ThreadSaveDigCategory {private static final int BATCH_COUTN 1000;Autowiredprivate Mapper mapper;public Future<Boolean> saveDigCategoryDatas(List<DigCategoryData> digCategoryDataList){//开始计时long startTime …...

【STM32】STM32的Cube和HAL生态

1.单片机软件开发的时代变化 1.单片机的演进过程 (1)第1代&#xff1a;4004、8008、Zilog那个年代&#xff08;大约1980年代之前&#xff09; (2)第2代&#xff1a;51、PIC8/16、AVR那个年代&#xff08;大约2005年前&#xff09; (3)第3代&#xff1a;51、PIC32、Cortex-M0、…...

汇编-EQU伪指令(数值替换)

EQU伪指令将一个符号名称与一个整数表达式或一个任意文本相关联&#xff0c; 它有3种格式 在第一种格式中&#xff0c; expression必须是一个有效的整数表达式。在第二种格式中&#xff0c; symbol是一个已存在的符号名称&#xff0c; 已经用或EQU定义过。在第三种格式中&…...

超声波俱乐部分享:Enter AI native application

11月5日&#xff0c;2023年第十四期超声波俱乐部内部分享会在北京望京举行。本期的主题是&#xff1a;Enter AI native application。 到场的嘉宾有&#xff1a;超声波创始人杨子超&#xff0c;超声波联合创始人、和牛商业创始人刘思雨&#xff0c;蓝驰创投合伙人刘勇&#xf…...

软件测试项目实战经验附视频以及源码【商城项目,app项目,电商项目,银行项目,医药项目,金融项目】(web+app+h5+小程序)

前言&#xff1a; ​​大家好&#xff0c;我是阿里测试君。 最近很多小伙伴都在面试&#xff0c;但是对于自己的项目经验比较缺少。阿里测试君再度出马&#xff0c;给大家找了一个非常适合练手的软件测试项目&#xff0c;此项目涵盖web端、app端、h5端、小程序端&#xff0c;…...

HarmonyOS应用开发-ArkTS基础知识

作者&#xff1a;杨亮Jerry 作为多年的大前端程序开发工作者&#xff0c;就目前的形式&#xff0c;个人浅见&#xff0c;在未来3-5年&#xff0c;移动端依旧是Android系统和iOS系统的天下。不过基于鸿蒙系统的应用开发还是值得我们去花点时间去了解下的&#xff0c;阅读并实践官…...

mybatis嵌套查询子集合只有一条数据

我们再用mybatis做嵌套查询时&#xff0c;有时会遇到子集合只有1条数据的情况&#xff0c;例如下这样&#xff1a; 数据库查询结果 xml <resultMap id"userMap" type"com.springboot.demo.test.entity.User"><id column"uid" property…...

Github 生成SSH秘钥及相关问题

1.生成过程参考&#xff1a;Github 生成SSH秘钥&#xff08;详细教程&#xff09;_github生成密钥controller节点生成ssh秘钥-CSDN博客 2.遇到的问题&#xff1a;GitHub Connect: kex_exchange_identification: Connection closed by remote host 注意&#xff1a;如果.ssh文…...

STM32外设系列—MPU6050角度传感器

&#x1f380; 文章作者&#xff1a;二土电子 &#x1f338; 关注公众号获取更多资料&#xff01; &#x1f438; 期待大家一起学习交流&#xff01; 文章目录 一、MPU6050简介二、MPU6050寄存器简介2.1 PWR_MGMT_1寄存器2.2 GYRO_CONFIG寄存器2.3 ACCEL_CONFIG寄存器2.4 PW…...

网站小程序分类目录网源码系统+会员登录注册功能 带完整搭建教程

大家好啊&#xff0c;源码小编今天来给大家分享一款网站小程序分类目录网源码系统会员登录注册功能 。 以下是核心代码图模块&#xff1a; 系统特色功能一览&#xff1a; 分类目录&#xff1a;系统按照不同的类别对网站进行分类&#xff0c;方便用户查找自己需要的网站。用户可…...

【Linux网络】手把手实操Linux系统网络服务DHCP

目录 一、什么是dhcp 二、详解dhcp的工作原理 三、dhcp的实操 第一步&#xff1a;3台机器的防火墙和安全机制都需要关闭&#xff01;&#xff01;&#xff01; 第二步&#xff1a;Linux下载dhcp软件&#xff0c;并查看配置文件位置 第三步&#xff1a;读配置文件&#xf…...

Huggingface网页解析和下载爬虫

解析网页&#xff1a; import requests from bs4 import BeautifulSoup# 目标网页URL url https://huggingface.co/internlm/internlm-20b/tree/main# 发送GET请求 response requests.get(url)# 检查请求是否成功 if response.status_code 200:# 使用BeautifulSoup解析HTML…...

C# Winform 自定义带SWITCH的卡片

1、创建卡片用户控件 在控件库中添加用户控件&#xff08;Windows窗体&#xff09;&#xff0c;命名为Card&#xff1b; 在属性/布局栏设置Size为148,128. 2、修改Card.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u…...

我用Devchat开发了公务员报名确认系统自动登录脚本,再也不用担心挤不进去了

#AI编程助手哪家好&#xff1f;DevChat“真”好用 # 演示效果 我用Devchat开发了公务员报名确认系统自动登录&#xff0c;再也不用担心挤不进去了 目录 演示效果前言粉丝独家专属红包码DevChat是什么&#xff1f;DevChat AI编程助手有哪些优势一、安装Vscode1、下载vscode链接…...

如何低门槛开发有趣实用的ZigBee产品?

一、什么是 Zigbee 协议&#xff1f; Zigbee 技术是一种连接距离短、功耗低、复杂程度低、数据传输量低的无线通信技术&#xff0c;其命名灵感源自于蜜蜂在群体中的信息传输。它主要通过网关与互联网进行通信&#xff0c;并嵌入各种智能设备&#xff0c;最终实现自动控制和远程…...

ChatGPT和API发生重大中断!

11月9日凌晨&#xff0c;OpenAI在官网发布&#xff0c;ChatGPT和API发生重大中断&#xff0c;导致全球所有用户无法正常使用&#xff0c;宕机时间超过2小时。 目前&#xff0c;OpenAI已经找到问题所在并进行了修复&#xff0c;但仍然不稳定&#xff0c;会继续进行安全监控。 …...

网络安全毕设简单的题目汇总

0 选题推荐 - 网络与信息安全篇 毕业设计是大家学习生涯的最重要的里程碑&#xff0c;它不仅是对四年所学知识的综合运用&#xff0c;更是展示个人技术能力和创新思维的重要过程。选择一个合适的毕业设计题目至关重要&#xff0c;它应该既能体现你的专业能力&#xff0c;又能满…...

思源宋体TTF:下一代开源中文字体架构与应用范式

思源宋体TTF&#xff1a;下一代开源中文字体架构与应用范式 【免费下载链接】source-han-serif-ttf Source Han Serif TTF 项目地址: https://gitcode.com/gh_mirrors/so/source-han-serif-ttf Source Han Serif TTF作为Google与Adobe联合打造的开源Pan-CJK字体&#xf…...

终极游戏自动化脚本:解放双手的完整指南

终极游戏自动化脚本&#xff1a;解放双手的完整指南 【免费下载链接】AzurLaneAutoScript Azur Lane bot (CN/EN/JP/TW) 碧蓝航线脚本 | 无缝委托科研&#xff0c;全自动大世界 项目地址: https://gitcode.com/gh_mirrors/az/AzurLaneAutoScript 想要在《碧蓝航线》中轻…...

跨越版本鸿沟:使用Oracle 19c OCI为DM8搭建连接Oracle 11G的DBLINK实战

1. 为什么需要高版本OCI连接低版本Oracle&#xff1f; 在国产化替代和数据迁移项目中&#xff0c;经常会遇到新旧数据库版本不兼容的问题。最近在帮客户做达梦数据库&#xff08;DM8&#xff09;与Oracle 11g的对接时&#xff0c;发现直接用11g的OCI驱动根本无法建立连接。经过…...

**发散创新:基于Python的文件API设计与高效读写实践**在现代软件开发中,**文件操作**是几乎所有应用的基础能

发散创新&#xff1a;基于Python的文件API设计与高效读写实践 在现代软件开发中&#xff0c;文件操作是几乎所有应用的基础能力之一。然而&#xff0c;传统的 open() read() / write() 模式虽然简单直接&#xff0c;但在面对复杂场景&#xff08;如大文件处理、流式传输、权限…...

企业级容器网络合规最后一道防线(Docker 27强制隔离模式启用倒计时72小时)

第一章&#xff1a;Docker 27强制网络隔离的合规背景与战略意义Docker 27 引入的强制网络隔离机制并非单纯的技术演进&#xff0c;而是对全球日益严苛的数据治理框架的主动响应。GDPR、CCPA、中国《数据安全法》及等保2.0均明确要求“最小化网络暴露面”与“逻辑域间访问可控”…...

原神60FPS限制终极解锁指南:突破性能瓶颈的完整解决方案

原神60FPS限制终极解锁指南&#xff1a;突破性能瓶颈的完整解决方案 【免费下载链接】genshin-fps-unlock unlocks the 60 fps cap 项目地址: https://gitcode.com/gh_mirrors/ge/genshin-fps-unlock 你是否曾经在原神游戏中感受到60FPS的限制&#xff1f;即使你的硬件配…...

【豆包从入门到精通共10篇】007、多模态应用:图像理解与生成能力探索

007、多模态应用:图像理解与生成能力探索 从一次深夜调试说起 上周三凌晨两点,我被测试组的紧急电话叫醒:“你们那个图像描述接口,传了张电路板照片,返回的结果是‘一只猫在玩毛线球’。” 我瞬间清醒——这问题可太致命了。我们的模型在标准数据集上准确率明明有92%,怎…...

别再手动拟合了!用CloudCompare的二次曲面功能,5分钟搞定点云曲面建模

点云建模革命&#xff1a;CloudCompare二次曲面拟合实战指南 当工程师第一次接触点云数据时&#xff0c;往往会被海量的三维坐标点震撼——这些来自激光扫描或摄影测量的数据点&#xff0c;精确记录了物体表面的几何特征&#xff0c;却也带来了巨大的处理挑战。特别是在需要从离…...

Phi-3.5-mini-instruct效果展示:中文诗歌创作、对联生成、节日祝福文案实录

Phi-3.5-mini-instruct效果展示&#xff1a;中文诗歌创作、对联生成、节日祝福文案实录 1. 模型简介与特点 Phi-3.5-mini-instruct 是一款轻量级但功能强大的中文文本生成模型&#xff0c;特别适合创意写作类任务。与常规问答模型不同&#xff0c;它在诗歌、对联、祝福文案等…...