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

Android Wi-Fi 连接失败日志分析

1. Android wifi 关键日志总结 (1) Wi-Fi 断开 (CTRL-EVENT-DISCONNECTED reason3) 日志相关部分&#xff1a; 06-05 10:48:40.987 943 943 I wpa_supplicant: wlan0: CTRL-EVENT-DISCONNECTED bssid44:9b:c1:57:a8:90 reason3 locally_generated1解析&#xff1a; CTR…...

iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享

平时用 iPhone 的时候&#xff0c;难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵&#xff0c;或者买了二手 iPhone 却被原来的 iCloud 账号锁住&#xff0c;这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

如何在看板中有效管理突发紧急任务

在看板中有效管理突发紧急任务需要&#xff1a;设立专门的紧急任务通道、重新调整任务优先级、保持适度的WIP&#xff08;Work-in-Progress&#xff09;弹性、优化任务处理流程、提高团队应对突发情况的敏捷性。其中&#xff0c;设立专门的紧急任务通道尤为重要&#xff0c;这能…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

ardupilot 开发环境eclipse 中import 缺少C++

目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

Spring数据访问模块设计

前面我们已经完成了IoC和web模块的设计&#xff0c;聪明的码友立马就知道了&#xff0c;该到数据访问模块了&#xff0c;要不就这俩玩个6啊&#xff0c;查库势在必行&#xff0c;至此&#xff0c;它来了。 一、核心设计理念 1、痛点在哪 应用离不开数据&#xff08;数据库、No…...

中医有效性探讨

文章目录 西医是如何发展到以生物化学为药理基础的现代医学&#xff1f;传统医学奠基期&#xff08;远古 - 17 世纪&#xff09;近代医学转型期&#xff08;17 世纪 - 19 世纪末&#xff09;​现代医学成熟期&#xff08;20世纪至今&#xff09; 中医的源远流长和一脉相承远古至…...

Python 实现 Web 静态服务器(HTTP 协议)

目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1&#xff09;下载安装包2&#xff09;配置环境变量3&#xff09;安装镜像4&#xff09;node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1&#xff09;使用 http-server2&#xff09;详解 …...