如何使用log4j2删除旧日志 [英] How to delete old logs with log4j2

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

问题描述

(F.Y.I.我已经在Internet上搜索了很多文档.我使用的是Storm-0.10.0-beta1.Storm中log4j2的配置文件是worker.xml)

( F.Y.I. I already searched out many documents in Internet. I'm using storm-0.10.0-beta1. Configuration file of log4j2 in Storm is worker.xml )

现在,我尝试使用log4j2.

Now, I try to use log4j2.

我正在寻找删除旧日志的方法,但找不到. 部分配置如下所示.

I'm searching out way of deleting old logs but I cannot find out. Part of configuration is like below.

    <RollingFile name="SERVICE_APPENDER"
             fileName="${sys:storm.home}/logs/${sys:logfile.name}.service"
             filePattern="${sys:storm.home}/logs/${sys:logfile.name}.service.%d{yyyyMMdd}">
        <PatternLayout>
            <pattern>${pattern}</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        </Policies>
        <DefaultRolloverStrategy max="9"/>
    </RollingFile>

首先,我希望删除3天以上的日志文件.

At first, I expected that log files which are older than 3 days are removed.

但是,实际上不是.

所以,我想知道是否有一种方法可以删除旧日志.

So, I wonder whether there is a way to remove old logs or not.

如果我还没有找到一种方法,请通知我.

If there is a way which I didn't catch yet, please notify me.

推荐答案

从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 any combination of:

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

需要更精细地控制要删除哪些文件的用户,可以使用任何受支持的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 should work:

  <DefaultRolloverStrategy>
    <!--
      * only files in the log folder, no sub folders
      * only rolled over log files (name match)
      * only files that are 4 days old or older
    -->
    <Delete basePath="${sys:storm.home}/logs/" maxDepth="1">
      <IfFileName glob="*.service.????????" />
      <IfLastModified age="4d" />
    </Delete>
  </DefaultRolloverStrategy>

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

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

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

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