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

突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合

强化学习&#xff08;Reinforcement Learning, RL&#xff09;是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程&#xff0c;然后使用强化学习的Actor-Critic机制&#xff08;中文译作“知行互动”机制&#xff09;&#xff0c;逐步迭代求解…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

vue3 定时器-定义全局方法 vue+ts

1.创建ts文件 路径&#xff1a;src/utils/timer.ts 完整代码&#xff1a; import { onUnmounted } from vuetype TimerCallback (...args: any[]) > voidexport function useGlobalTimer() {const timers: Map<number, NodeJS.Timeout> new Map()// 创建定时器con…...

C++.OpenGL (10/64)基础光照(Basic Lighting)

基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...

【HTML-16】深入理解HTML中的块元素与行内元素

HTML元素根据其显示特性可以分为两大类&#xff1a;块元素(Block-level Elements)和行内元素(Inline Elements)。理解这两者的区别对于构建良好的网页布局至关重要。本文将全面解析这两种元素的特性、区别以及实际应用场景。 1. 块元素(Block-level Elements) 1.1 基本特性 …...

均衡后的SNRSINR

本文主要摘自参考文献中的前两篇&#xff0c;相关文献中经常会出现MIMO检测后的SINR不过一直没有找到相关数学推到过程&#xff0c;其中文献[1]中给出了相关原理在此仅做记录。 1. 系统模型 复信道模型 n t n_t nt​ 根发送天线&#xff0c; n r n_r nr​ 根接收天线的 MIMO 系…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...

08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险

C#入门系列【类的基本概念】&#xff1a;开启编程世界的奇妙冒险 嘿&#xff0c;各位编程小白探险家&#xff01;欢迎来到 C# 的奇幻大陆&#xff01;今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类&#xff01;别害怕&#xff0c;跟着我&#xff0c;保准让你轻松搞…...