当前位置: 首页 > 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;会继续进行安全监控。 …...

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

反射获取方法和属性

Java反射获取方法 在Java中&#xff0c;反射&#xff08;Reflection&#xff09;是一种强大的机制&#xff0c;允许程序在运行时访问和操作类的内部属性和方法。通过反射&#xff0c;可以动态地创建对象、调用方法、改变属性值&#xff0c;这在很多Java框架中如Spring和Hiberna…...

【C++从零实现Json-Rpc框架】第六弹 —— 服务端模块划分

一、项目背景回顾 前五弹完成了Json-Rpc协议解析、请求处理、客户端调用等基础模块搭建。 本弹重点聚焦于服务端的模块划分与架构设计&#xff0c;提升代码结构的可维护性与扩展性。 二、服务端模块设计目标 高内聚低耦合&#xff1a;各模块职责清晰&#xff0c;便于独立开发…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

MySQL账号权限管理指南:安全创建账户与精细授权技巧

在MySQL数据库管理中&#xff0c;合理创建用户账号并分配精确权限是保障数据安全的核心环节。直接使用root账号进行所有操作不仅危险且难以审计操作行为。今天我们来全面解析MySQL账号创建与权限分配的专业方法。 一、为何需要创建独立账号&#xff1f; 最小权限原则&#xf…...

Linux 中如何提取压缩文件 ?

Linux 是一种流行的开源操作系统&#xff0c;它提供了许多工具来管理、压缩和解压缩文件。压缩文件有助于节省存储空间&#xff0c;使数据传输更快。本指南将向您展示如何在 Linux 中提取不同类型的压缩文件。 1. Unpacking ZIP Files ZIP 文件是非常常见的&#xff0c;要在 …...

解析奥地利 XARION激光超声检测系统:无膜光学麦克风 + 无耦合剂的技术协同优势及多元应用

在工业制造领域&#xff0c;无损检测&#xff08;NDT)的精度与效率直接影响产品质量与生产安全。奥地利 XARION开发的激光超声精密检测系统&#xff0c;以非接触式光学麦克风技术为核心&#xff0c;打破传统检测瓶颈&#xff0c;为半导体、航空航天、汽车制造等行业提供了高灵敏…...

HybridVLA——让单一LLM同时具备扩散和自回归动作预测能力:训练时既扩散也回归,但推理时则扩散

前言 如上一篇文章《dexcap升级版之DexWild》中的前言部分所说&#xff0c;在叠衣服的过程中&#xff0c;我会带着团队对比各种模型、方法、策略&#xff0c;毕竟针对各个场景始终寻找更优的解决方案&#xff0c;是我个人和我司「七月在线」的职责之一 且个人认为&#xff0c…...

门静脉高压——表现

一、门静脉高压表现 00:01 1. 门静脉构成 00:13 组成结构&#xff1a;由肠系膜上静脉和脾静脉汇合构成&#xff0c;是肝脏血液供应的主要来源。淤血后果&#xff1a;门静脉淤血会同时导致脾静脉和肠系膜上静脉淤血&#xff0c;引发后续系列症状。 2. 脾大和脾功能亢进 00:46 …...