问题
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>
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..
however I just did a test run and it got to 5 files before I stopped it....
any idea what could be causing this?
回答1:
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>
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
来源:https://stackoverflow.com/questions/44588333/log4j2-default-rollover-strategy-not-deleting-logs