log4j2默认过渡策略不删除日志 [英] log4j2 Default rollover strategy not deleting logs
问题描述
我的log4j2设置了默认的过渡策略,如下所示-
I have log4j2 with a default rollover strategy set up like this -
<RollingFile name="RollingFile" fileName="cc" filePattern="logs/${baseFileName}-%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="logs/">
<IfFileName glob="logs/${baseFileName}-*.log" />
<IfLastModified age="2d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
所以当正确使用超过2天时,应该删除最早的文件吗?
so it should be deleting the oldest files when it gets to over 2 days old correct?
我的日志文件存储在项目的基本路径中的日志文件中.
my log files are stored in the base path of the project in a folder called logs..
但是我只是进行了一次测试,在停止它之前它已经达到了5个文件....
however I just did a test run and it got to 5 files before I stopped it....
你知道是什么原因造成的吗?
any idea what could be causing this?
推荐答案
请检查以下配置以删除旧文件: DefaultRolloverStrategy max ="10"表示每天最多可以创建10个文件. 您可以使用IfAccumulatedFileCount超出="2"来控制始终存在多少个文件. 将指定文件的使用期限,如果文件总数大于2(如IfAccumulatedFileCount中所指定),则从今天起2天之前的文件将被删除.
Please check the below configuration for deleting old files: DefaultRolloverStrategy max="10" means daily 10 files can be created max. You can use IfAccumulatedFileCount exceeds="2" to control how many files will be present at all time. will specify the age of the file, the files older than 2 days from present day will be deleted if the total number of files are grater than 2 (as specified in IfAccumulatedFileCount ).
<DefaultRolloverStrategy max="100">
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log">
<IfLastModified age="2d">
<IfAny>
<IfAccumulatedFileCount exceeds="2" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
也许它在错误的路径中搜索要删除的文件,或者您的日志文件存在时间不到2天. 您正在使用哪个版本的log4j?它将适用于Log4j-2.5及更高版本
Maybe it is searching the file to delete in the incorrect path or your log files are not 2 day old. which version of log4j you are using? it will work for Log4j-2.5 and above
这篇关于log4j2默认过渡策略不删除日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!