【问题标题】:log4j2 unable to delete old fileslog4j2 无法删除旧文件
【发布时间】:2017-12-20 21:21:13
【问题描述】:

我到处寻找答案。归档文件数超过10个后我必须删除文件。

这是我的 log4j2.xml

  <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
            filePattern="C:/temp/logs/app.log.%i">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1000 kb" />
            </Policies>
            <DefaultRolloverStrategy max="10">
                <Delete basePath="C:/temp/logs/" maxDepth="1">
                    <IfFileName glob="app*.log*">
                        <IfLastModified age="2m">
                            <IfAny>
                                <IfAccumulatedFileCount exceeds="11" />
                            </IfAny>
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
</Configuration>

但它不会删除文件夹中的所有文件。例如在下面的截图中,它并没有删除旧的日志文件:

logs

我浏览了所有文档并进行了大量讨论,但我无法解决这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: java logging log4j2


    【解决方案1】:

    这是一个类似的问题,可能会为您尝试做的事情提供一些帮助 = Log4j2 - Configure RolloverStrategy to delete old log files

    cmets 应该解释 Delete 下的每个条件会做什么。在您的示例中,如果您希望删除 app-log-12-20*,那么您需要删除 IfLastModified 或 IfAccumulatedFileCount - 您将看到文件被删除

     <Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
            filePattern="C:/temp/logs/app.log.%i">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1000 kb" />
            </Policies>
            <DefaultRolloverStrategy max="10">
                <Delete basePath="C:/temp/logs/" maxDepth="1">
                    <IfAll> 
    
                        <!-- Looks for file names starting with app*.log*. Any file including app.log.1 will satisfy this condition. Could delete current log app.log -->
                        <IfFileName glob="app*.log*" />
                        <!-- Looks for files that are older than 2 mins --> 
                        <IfLastModified age="2m" />  
                         <!-- This means there need to be 11 fails that satisfy the above conditions, that i.e. their name is app*log* and have time stamp greater than 2 mins. Keeps the most recent files that satisfy the condition -->
                        <IfAccumulatedFileCount exceeds="11" /> 
                    </IfAll>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
    

    【讨论】:

    • 谢谢Saifuddin Merchant,但是如果日志超过2分钟或文件数达到10,则以先到者为准。但即使我删除了 ,它仍然不会删除旧文件。有 16 个 app*.log* 文件,其余 2 个是旧文件(6 天前)。在给定时间不应超过 10 个文件(如果我删除了 )。但是,如果我手动删除旧文件,它会起作用,但如果我需要将代码投入生产,那不是删除旧日志文件的选项。
    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2016-04-14
    相关资源
    最近更新 更多