java Log4j - DailyRollingFileAppender - 滚动文件在一段时间后是否被删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13864899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:14:05  来源:igfitidea点击:

Log4j - DailyRollingFileAppender - Are rolled files deleted after some amount of time?

javalogginglog4j

提问by sixtyfootersdude

This answerimplies that logs are kept for 7 days. Is this configurable?

此答案意味着日志会保留 7 天。这个可以配置吗?

This Java Ranch Post implies that files are never deleted.

这个Java Ranch Post暗示文件永远不会被删除。

This site also agrees that log files are never removed:

该站点还同意永远不会删除日志文件:

If you are trying to use the Apache Log4J DailyRollingFileAppender for a daily log file, you may need to want to specify the maximum number of files which should be kept. Just like rolling RollingFileAppender supports maxBackupIndex. But the current version of Log4j (Apache log4j 1.2.16) does not provide any mechanism to delete old log files if you are using DailyRollingFileAppender. I tried to make small modifications in the original version of DailyRollingFileAppender to add maxBackupIndex property. So, it would be possible to clean up old log files which may not be required for future usage.

如果您尝试将 Apache Log4J DailyRollingFileAppender 用于每日日志文件,您可能需要指定应保留的最大文件数。就像滚动 RollingFileAppender 支持 maxBackupIndex 一样。但是,如果您使用 DailyRollingFileAppender,当前版本的 Log4j(Apache log4j 1.2.16)不提供任何删除旧日志文件的机制。我尝试在 DailyRollingFileAppender 的原始版本中进行小的修改以添加 maxBackupIndex 属性。因此,可以清理将来可能不需要的旧日志文件。

Source

来源

I can't find an authoritative answer and I don't want to wait 7 days to see if my logs are deleted.

我找不到权威答案,我不想等待 7 天才能查看我的日志是否被删除。

采纳答案by Robert H

See this this postregarding Log4J deletes. In short it appears that dailyRollingFileAppender cannot. But perhaps you might want to look at switching to Logback. It was written by the same guy and can do what your looking for.

请参阅这篇关于 Log4J 删除的帖子。简而言之,dailyRollingFileAppender 似乎不能。但也许你可能想看看切换到Logback。它是由同一个人编写的,可以满足您的需求。

I use the following appender to maintain 30 days of HTML logs:

我使用以下 appender 来维护 30 天的 HTML 日志:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>Logs\logFile.html</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.html</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    <charset>UTF-8</charset>
        <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
        </layout>           
    </encoder>
</appender>

The relevant sectionfrom the manual states the following on maxHistory:

手册中的相关部分说明了以下内容maxHistory

The optional maxHistory property controls the maximum number of archive files to keep, deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept with files older than 6 months deleted. Note as old archived log files are removed, any folders which were created for the purpose of log file archiving will be removed as appropriate.

可选的 maxHistory 属性控制要保留的存档文件的最大数量,删除旧文件。例如,如果您指定每月滚动更新,并将 maxHistory 设置为 6,则将保留 6 个月的存档文件,并删除超过 6 个月的文件。请注意,随着旧存档日志文件被删除,任何为日志文件存档目的而创建的文件夹都将被适当删除。

Logback also has a Log4J properties translator available hereto help with the transistion. As well as an entire chapter in their manualdevoted to switching from Log4J.

还有的logback有一个可用的log4j属性翻译在这里与transistion帮助。以及他们手册中的整章专门用于从 Log4J 切换。