如何删除较旧的log4j2日志,最多保留10个文件? [英] How to delete older rolled over log4j2 logs, keeping up to 10 files?

查看:1281
本文介绍了如何删除较旧的log4j2日志,最多保留10个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是:

  • 最多10个日志文件,总计
  • 每个日志文件,大小不超过50MB.

因此,logs文件夹永远不会超过(50MB * 10)= 500MB.

Thus the logs folder never grows over (50MB *10 )= 500MB.

但是似乎我的log4j2配置未正确完成.

But it seems my log4j2 config is not properly done.

正在发生的事情是:

  • 日志会在50 MB后重新滚动
  • 但是每天最多保存10条日志
  • 因此,日志文件夹中保存的日志文件数量没有限制(例如,自2天内以来,已收集了20个日志,每个日志50mb)

这是配置:

<Configuration status="WARN">
    <Appenders>
        <RollingFile name="RollingFile" fileName="log/my.log" filePattern="log/my-%d{MM-dd-yyyy}-%i.log">
          <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
          </PatternLayout>
          <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
          </Policies>
          <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

我在做什么错了?

推荐答案

从2.5开始,Log4j支持

Since 2.5, Log4j supports a custom Delete action that is executed on every rollover.

您可以控制通过以下方式删除哪些文件

You can control which files are deleted by:

  1. 名称(匹配全局 regex )
  2. 计数(仅保留最近的3个")
  3. 大小(仅保留最大为500MB的最新文件")
  1. Name (matching a glob or a regex)
  2. Age ("delete if 14 days old or older")
  3. Count ("keep only the most recent 3")
  4. Size ("keep only the most recent files up to 500MB")

以上内容可以组合使用.与其仅指定大小条件以使磁盘使用量最大保持在500MB以下,还不如指定名称,这样一个好主意,这样您就不会无意间删除不相关的文件.

The above can be combined. Instead of only specifying a size condition to keep disk usage down to max 500MB, it's a good idea to also match the name so you don't inadvertently delete unrelated files.

需要更精细地控制要删除哪些文件的用户,可以使用任何受支持的JSR-223脚本语言来指定脚本条件.

Users who need even more fine-grained control over which files to delete can specify a script condition using any supported JSR-223 scripting language.

请查看文档,其中包含三个可能有用的完整示例.

Please check out the documentation, it has three full examples that may be useful.

对于您的问题,此代码段可能有效:

For your question, this snippet may work:

  <DefaultRolloverStrategy>
    <!--
      * only files in the log folder, no sub folders
      * only rolled over log files (name match)
      * either when more than 10 matching files exist or when the max disk usage is exceeded
    -->
    <Delete basePath="log" maxDepth="1">
      <IfFileName glob="my-??-??-????-*.log">
        <IfAny>
          <IfAccumulatedFileSize exceeds="500 MB" />
          <IfAccumulatedFileCount exceeds="10" />
        </IfAny>
      </IfFileName>
    </Delete>
  </DefaultRolloverStrategy>

请注意,您可以在滚动以使其占用更少的磁盘空间.

As an aside, note that you can compress log files on rollover to make them take up less disk space.

最后,要小心!无法恢复以这种方式删除的文件. :-)

Finally, be careful! There is no way to recover files deleted this way. :-)

这篇关于如何删除较旧的log4j2日志,最多保留10个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆