当前位置:网站首页>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 .
边栏推荐
- C语言:函数栈帧
- Spark RPC
- Static关键字的三种用法
- [正则表达式] 匹配开头和结尾
- Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
- 初识结构体
- How to edit a framework resource file separately
- [daily question 1] 558. Intersection of quadtrees
- Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
- Pytorch replaces some components in numpy. / / please indicate the source of the reprint
猜你喜欢
随机推荐
Spark lazy list files 的实现
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
Implement custom spark optimization rules
Leetcode 456.132 mode monotone stack /medium
Singles cup, web:web check in
Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
折半查找
js寻找数组中的最大和最小值(Math.max()方法)
Leetcode 341. flattened nested list iterator DFS, stack / medium
[0 basic operations research] [super detail] column generation
Leetcode 81. search rotation sort array II binary /medium
Jump to the specified position when video continues playing
HaoChen CAD building 2022 software installation package download and installation tutorial
折半插入排序
实体类(VO,DO,DTO)的划分
[正则表达式] 匹配多个字符
Go language learning notes (1)
C语言:动态内存函数
JS uses unary operators to simplify string to number conversion
C:什么是函数中的返回值(转)









