【问题标题】:Log4j2 DefaultRolloverStrategy Configuration deletes log files but not empty foldersLog4j2 DefaultRolloverStrategy 配置删除日志文件但不删除空文件夹
【发布时间】:2019-04-18 19:16:12
【问题描述】:

我将 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>

删除文件夹中的所有文件后,如何删除文件夹? 谢谢!

【问题讨论】:

    标签: java xml logging log4j log4j2


    【解决方案1】:

    您在 log4j2 中没有用于删除空文件夹的内置选项。您可以查看此StackOverflow answer 和此log4j2 issues discussion。

    我想说处理这个问题的最简单方法是运行一个 cron,而不管 log4j 配置如何。我用crontab。

    【讨论】:

      【解决方案2】:

      默认情况下,删除操作不会删除文件夹,即使文件夹为空。 如果要根据大小和年龄删除文件夹,可以使用脚本来执行此操作,请参阅官方文档以获取帮助: http://logging.apache.org/log4j/2.x/manual/appenders.html#ScriptCondition

      只需返回要从脚本中删除的文件夹。

      【讨论】:

        【解决方案3】:

        我有同样的问题/客户要求并这样解决:

        <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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-23
          • 2014-07-02
          • 2021-11-24
          相关资源
          最近更新 更多