当前位置:网站首页>Extended log4j supports the automatic deletion of log files according to time division and expired files
Extended log4j supports the automatic deletion of log files according to time division and expired files
2022-07-27 15:38:00 【wankunde】
In order to meet our needs , Let's take a look at the original Log4j How to manage log files .
RollingFileAppender
stay log4j in Appender Be responsible for writing the log to the log file , The service runs for a long time in the actual production environment , There will be many log files , So we usually use
RollingFileAppenderTo cut the log according to the rules , Write to different log files .stay log4j The built-in class is
org.apache.log4j.RollingFileAppender, The default function of this class is relatively simple , Through parameters maxBackupIndex To configure Rolling File The number of ,maxFileSize Configure the size of each log file , Then scroll through the output log .stay
log4j:apache-log4j-extras:1.2.17In this expansion package , A new class is re extendedorg.apache.log4j.rolling.RollingFileAppender. ThisRollingFileAppenderYou can check the file size or print the log according to different parameters Rolling File, But this, this Appender The function of deleting log files is lost ( No at all maxBackupIndex Parameters …).
TriggeringPolicy
org.apache.log4j.rolling.RollingFileAppenderinTriggeringPolicyTo indicate progress Rolling Log File The strategy of . The official offers twoorg.apache.log4j.rolling.SizeBasedTriggeringPolicyandorg.apache.log4j.rolling.TimeBasedRollingPolicyTwo strategies are implemented , Users can also expand new Policy.
TriggeringPolicy.isTriggeringEvent()
- When
TriggeringPolicy.isTriggeringEvent()The method is judged as true when , Conduct Rolling File operation ; o.g.l.r.SizeBasedTriggeringPolicyMediumisTriggeringEvent()The judgment condition of is that the log file size is greater than maxFileSize.o.g.l.r.TimeBasedRollingPolicyMediumisTriggeringEvent()It is based on the system time and FileNamePattern Parameters to calculate Rolling File The name of the file , When Rolling File The file name of has changed , Conduct Rolling File operation ;
rollover
- When
TriggeringPolicyOfisTriggeringEvent()Method returns True after ,o.a.l.rolling.RollingFileAppenderStart executionrollover()Method rollover()Method wait for the last rollover End of normal execution- adopt policy Of
rollingPolicy.rollover(getFile())Method , return RolloverDescription object , Express Rolling File A series of actions . - Synchronous execution
RolloverDescriptionMedium Synchronous Action operation ( For example, rename the file ) And wait for success . - Start asynchronous thread , perform
RolloverDescriptionMedium Asynchronous Action operation ( For example, compress log files ). - Action The concept of can be a single task operation , It can also be a group of task operations (
CompositeAction)
summary
about Long Running Of Java process , We need more than Rolling File The operation of , We also need to understand history Log File Clean up . So I referred to TimeBasedRollingPolicy Code , Realized ExtendedTimeBasedRollingPolicy class . In the original class ,asynchronous It's alone compressAction Be responsible for compressing log files ( When conditions are met ), Let's replace this object with CompositeAction, Then add the one we want to be responsible for cleaning up the files PurgeExpiredLogsAction that will do . stay PurgeExpiredLogsAction in , We can flexibly use the file name of the log , Number of log files , File size and other conditions , Delete the historical log file .
边栏推荐
- $router.back(-1)
- js寻找数组中的最大和最小值(Math.max()方法)
- 【云享读书会第13期】音频文件的封装格式和编码格式
- C语言中交换两数的方法
- Cap theory and base theory
- 【剑指offer】面试题54:二叉搜索树的第k大节点
- C语言:三子棋游戏
- Dan bin Investment Summit: on the importance of asset management!
- Jump to the specified position when video continues playing
- Using Prometheus to monitor spark tasks
猜你喜欢

数组名是首元素地址吗?

复杂度分析
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

【剑指offer】面试题45:把数组排成最小的数

With just two modifications, apple gave styleganv2 3D generation capabilities

Leetcode 240. search two-dimensional matrix II medium

Spark 3.0 DPP实现逻辑

Pictures to be delivered

Dan bin Investment Summit: on the importance of asset management!

Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
随机推荐
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
C:浅谈函数
QT (XIII) qchart drawing line chart
Two stage submission and three stage submission
Leetcode 781. rabbit hash table in forest / mathematical problem medium
shell脚本读取文本中的redis命令批量插入redis
Unity3d learning note 10 - texture array
[TensorBoard] OSError: [Errno 22] Invalid argument处理
Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
Fluent -- layout principle and constraints
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
Use double stars instead of math.pow()
【剑指offer】面试题46:把数字翻译成字符串——动态规划
设置提示框位置随鼠标移动,并解决提示框显示不全的问题
Summer Challenge harmonyos realizes a hand-painted board
Leetcode 190. reverse binary bit operation /easy
Leetcode 81. search rotation sort array II binary /medium
How to edit a framework resource file separately