当前位置:网站首页>扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
2022-07-27 14:23:00 【wankunde】
为了实现我们的需求,我们先看一下原始的Log4j 是如何实现日志文件的管理的。
RollingFileAppender
在log4j中Appender 负责将日志写入日志文件中,实际生产环境中服务长期运行,日志文件会非常多,所以我们一般使用
RollingFileAppender来将日志根据规则进行切割,写入不同的日志文件中。在log4j内置的类是
org.apache.log4j.RollingFileAppender,这个类默认功能比较单一,通过参数 maxBackupIndex 配置Rolling File的个数,maxFileSize 配置每个日志文件的大小,然后滚动输出日志。在
log4j:apache-log4j-extras:1.2.17这个扩展包中,又重新扩展了一个新的类org.apache.log4j.rolling.RollingFileAppender。这个RollingFileAppender可以根据检查文件大小或者打印日志的时间不同参数进行Rolling File,但是这个这个Appender 丢失了删除日志文件的功能(完全没有maxBackupIndex 参数了…)。
TriggeringPolicy
org.apache.log4j.rolling.RollingFileAppender中TriggeringPolicy来表示进行Rolling Log File的策略。官方提供了两种org.apache.log4j.rolling.SizeBasedTriggeringPolicy和org.apache.log4j.rolling.TimeBasedRollingPolicy两种策略实现,用户也可以根据自己的需求扩展新的Policy。
TriggeringPolicy.isTriggeringEvent()
- 当
TriggeringPolicy.isTriggeringEvent()方法判断为true时,进行Rolling File操作; o.g.l.r.SizeBasedTriggeringPolicy中的isTriggeringEvent()的判断条件是日志文件大小大于 maxFileSize。o.g.l.r.TimeBasedRollingPolicy中的isTriggeringEvent()是根据系统时间和FileNamePattern 参数来计算Rolling File的文件名,当Rolling File的文件名发生变化,进行Rolling File操作;
rollover
- 当
TriggeringPolicy的isTriggeringEvent()方法返回True后,o.a.l.rolling.RollingFileAppender开始执行rollover()方法 rollover()方法先等待上一次的rollover正常执行结束- 通过policy 的
rollingPolicy.rollover(getFile())方法,返回 RolloverDescription对象,表示Rolling File的一系列动作。 - 同步执行
RolloverDescription中的 Synchronous Action操作(例如重明命名文件)并等待成功。 - 启动异步线程,执行
RolloverDescription中的 Asynchronous Action操作(例如压缩日志文件等)。 - Action的概念可以是一个单独的任务操作,也可以是一组任务操作(
CompositeAction)
总结
对于Long Running的Java进程,我们不仅仅需要进行Rolling File的操作,还需要对历史Log File进行清理。所以我参考了 TimeBasedRollingPolicy 代码,实现了ExtendedTimeBasedRollingPolicy类。原来的类中,asynchronous 就是单独的 compressAction 负责对日志文件进行压缩(满足条件时),我们将这个对象换成 CompositeAction,再加入我们想要的负责清理文件的 PurgeExpiredLogsAction 即可。在 PurgeExpiredLogsAction 中,我们可以灵活的根据日志的文件名,日志的文件的个数,文件的大小等条件,进行历史日志文件的删除。
边栏推荐
- MOS管防止电源反接的原理
- Internship: compilation of other configuration classes
- Discussion on STM32 power down reset PDR
- 修改 Spark 支持远程访问OSS文件
- USB接口电磁兼容(EMC)解决方案
- Dialog manager Chapter 3: create controls
- DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
- 初探STM32掉电复位PDR
- Introduction of the connecting circuit between ad7606 and stm32
- 《剑指Offer》数组中的逆序对
猜你喜欢
MOS管防止电源反接的原理
Principle of MOS tube to prevent reverse connection of power supply

Zhou Hongyi: if the digital security ability is backward, it will also be beaten

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

Spark Bucket Table Join

After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues

Kotlin的基础用法

AssetBundle如何打包

How to edit a framework resource file separately

Basic usage of kotlin
随机推荐
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
TCC
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers
Problem solving in magic tower project
学习Parquet文件格式
JMeter recording interface automation
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
How to edit a framework resource file separately
Reading notes of lifelong growth (I)
Introduction to STM32 learning can controller
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Leetcode 81. search rotation sort array II binary /medium
DevEco Studio2.1运行项目报错
shell脚本读取文本中的redis命令批量插入redis
仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
Unity 鼠标控制第一人称摄像机视角
Notice of Shenzhen Municipal Bureau of human resources and social security on the issuance of employment related subsidies for people out of poverty