聊聊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ülcü*/
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ülcü*/
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…...

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

文件缓存的读写
文件系统的读写,其实就是调用系统函数 read 和 write。下面的代码就是 read 和 write 的系统调用,在内核里面的定义。 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代:4004、8008、Zilog那个年代(大约1980年代之前) (2)第2代:51、PIC8/16、AVR那个年代(大约2005年前) (3)第3代:51、PIC32、Cortex-M0、…...

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

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

软件测试项目实战经验附视频以及源码【商城项目,app项目,电商项目,银行项目,医药项目,金融项目】(web+app+h5+小程序)
前言: 大家好,我是阿里测试君。 最近很多小伙伴都在面试,但是对于自己的项目经验比较缺少。阿里测试君再度出马,给大家找了一个非常适合练手的软件测试项目,此项目涵盖web端、app端、h5端、小程序端,…...

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

mybatis嵌套查询子集合只有一条数据
我们再用mybatis做嵌套查询时,有时会遇到子集合只有1条数据的情况,例如下这样: 数据库查询结果 xml <resultMap id"userMap" type"com.springboot.demo.test.entity.User"><id column"uid" property…...
Github 生成SSH秘钥及相关问题
1.生成过程参考:Github 生成SSH秘钥(详细教程)_github生成密钥controller节点生成ssh秘钥-CSDN博客 2.遇到的问题:GitHub Connect: kex_exchange_identification: Connection closed by remote host 注意:如果.ssh文…...

STM32外设系列—MPU6050角度传感器
🎀 文章作者:二土电子 🌸 关注公众号获取更多资料! 🐸 期待大家一起学习交流! 文章目录 一、MPU6050简介二、MPU6050寄存器简介2.1 PWR_MGMT_1寄存器2.2 GYRO_CONFIG寄存器2.3 ACCEL_CONFIG寄存器2.4 PW…...

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

【Linux网络】手把手实操Linux系统网络服务DHCP
目录 一、什么是dhcp 二、详解dhcp的工作原理 三、dhcp的实操 第一步:3台机器的防火墙和安全机制都需要关闭!!! 第二步:Linux下载dhcp软件,并查看配置文件位置 第三步:读配置文件…...
Huggingface网页解析和下载爬虫
解析网页: 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、创建卡片用户控件 在控件库中添加用户控件(Windows窗体),命名为Card; 在属性/布局栏设置Size为148,128. 2、修改Card.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u…...

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

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

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

接口测试中缓存处理策略
在接口测试中,缓存处理策略是一个关键环节,直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性,避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明: 一、缓存处理的核…...
【网络】每天掌握一个Linux命令 - iftop
在Linux系统中,iftop是网络管理的得力助手,能实时监控网络流量、连接情况等,帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

为什么需要建设工程项目管理?工程项目管理有哪些亮点功能?
在建筑行业,项目管理的重要性不言而喻。随着工程规模的扩大、技术复杂度的提升,传统的管理模式已经难以满足现代工程的需求。过去,许多企业依赖手工记录、口头沟通和分散的信息管理,导致效率低下、成本失控、风险频发。例如&#…...

使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台
🎯 使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台 📌 项目背景 随着大语言模型(LLM)的广泛应用,开发者常面临多个挑战: 各大模型(OpenAI、Claude、Gemini、Ollama)接口风格不统一;缺乏一个统一平台进行模型调用与测试;本地模型 Ollama 的集成与前…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)
目录 一、👋🏻前言 二、😈sinx波动的基本原理 三、😈波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、🌊波动优化…...
Java 二维码
Java 二维码 **技术:**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...
音视频——I2S 协议详解
I2S 协议详解 I2S (Inter-IC Sound) 协议是一种串行总线协议,专门用于在数字音频设备之间传输数字音频数据。它由飞利浦(Philips)公司开发,以其简单、高效和广泛的兼容性而闻名。 1. 信号线 I2S 协议通常使用三根或四根信号线&a…...
CSS | transition 和 transform的用处和区别
省流总结: transform用于变换/变形,transition是动画控制器 transform 用来对元素进行变形,常见的操作如下,它是立即生效的样式变形属性。 旋转 rotate(角度deg)、平移 translateX(像素px)、缩放 scale(倍数)、倾斜 skewX(角度…...

Unity VR/MR开发-VR开发与传统3D开发的差异
视频讲解链接:【XR马斯维】VR/MR开发与传统3D开发的差异【UnityVR/MR开发教程--入门】_哔哩哔哩_bilibili...
深度解析:etcd 在 Milvus 向量数据库中的关键作用
目录 🚀 深度解析:etcd 在 Milvus 向量数据库中的关键作用 💡 什么是 etcd? 🧠 Milvus 架构简介 📦 etcd 在 Milvus 中的核心作用 🔧 实际工作流程示意 ⚠️ 如果 etcd 出现问题会怎样&am…...