![](/img/trans.png)
[英]Log4j2 DefaultRolloverStrategy Configuration does not delete old logs
[英]Log4j2 DefaultRolloverStrategy Configuration deletes log files but not empty folders
我將 log4j2 用於我的應用程序日志,我正在使用 XML,下面的配置用於日志。 我將日志存儲在以當前日期命名的文件夾中。 每天都會創建一個名為“2018-11-15”的新文件夾,並將日志存儲在其中。 該代碼根據大小和年齡刪除日志文件,但從文件夾中刪除這些文件后,不會刪除空文件夾(2018-11-15)。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="baseDir">logs</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="100">
<!--
Nested conditions: the inner condition is only evaluated on files
for which the outer conditions are true.
-->
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="100 GB" />
<IfAccumulatedFileCount exceeds="10" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
刪除文件夾中的所有文件后,如何刪除文件夾? 謝謝!
您在 log4j2 中沒有用於刪除空文件夾的內置選項。 您可以查看此StackOverflow 答案和此log4j2 問題討論。
我會說處理這個問題的最簡單方法是運行一個 cron,不管 log4j 配置如何。 我使用crontab 。
默認情況下,即使文件夾為空,刪除操作也不會刪除文件夾。 如果您想根據大小和年齡刪除文件夾,您可以使用腳本來執行此操作,請參閱官方文檔尋求幫助: http : //logging.apache.org/log4j/2.x/manual/appenders.html#ScriptCondition
只需返回要從腳本中刪除的文件夾即可。
我遇到了同樣的問題/客戶要求並像這樣解決了它:
<RollingRandomAccessFile name="ROLLING_TEXT_FILE_APPENDER" fileName="${sys:ehr.logDir}/ehr.log"
filePattern="${sys:ehr.logDir}/$${date:yyyy-MM}/ehr-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout pattern="%d %p [%t(%T)] %c{-3} - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50MB"/>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:ehr.logDir}" maxDepth="2">
<IfFileName glob="*/ehr-*.log.gz" />
<IfLastModified age="30d" />
</Delete>
<Delete basePath="${sys:ehr.logDir}" maxDepth="2">
<ScriptCondition>
<Script name="GroovyCondition" language="groovy"><![CDATA[
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes;
List<PathWithAttributes> result = new ArrayList<PathWithAttributes>();
statusLogger.trace 'SCRIPT: Checking for empty folders in base path: ' + basePath
Files.list(basePath).filter(p -> p.toFile().isDirectory()).forEach(p ->{
statusLogger.trace 'SCRIPT: Testing if folder is empty: ' + p
try(DirectoryStream<Path> dirStream = Files.newDirectoryStream(p)) {
// is directory empty?
if (!dirStream.iterator().hasNext()) {
statusLogger.trace 'SCRIPT: Returning empty folder for deletion: ' + p
BasicFileAttributes attributes = Files.readAttributes(p, BasicFileAttributes.class);
result.add(new PathWithAttributes(p, attributes));
}
}
})
return result;
]]>
</Script>
</ScriptCondition>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.