<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<Configuration status="INFO" monitorInterval="120">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p %c{1.}:%L[%t] %m%n" />
</Console>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="${log4j:configParentLocation}/../logs/info.log" filePattern="${log4j:configParentLocation}/../logs/info-%d{MM-dd-yyyy}.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${log4j:configParentLocation}/../logs/" maxDepth="1">
<IfFileName glob="info-*.log.gz" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<logger name="org.springframework" level="INFO" />
<Root level="DEBUG">
<AppenderRef ref="RollingRandomAccessFile" />
</Root>
</Loggers>
</Configuration>
关注其中的DefaultRolloverStrategy配置内容,按照此配置会自动清除Delete元素中指定basePath下超出30天的info-*.log.gz配置文件。由于是指定目录,指定文件名格式,指定日期范围,所以是可以清除非工程目录下的文件,所以一定不要配置错了。