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

本文是小编为大家收集整理的关于如何用log4j2删除旧的日志的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

(F.Y.I.我已经在Internet上搜索了许多文档.

现在,我尝试使用log4j2.

我正在搜索删除旧日志的方式,但我找不到. 配置的一部分如下.

    <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天的日志文件.

但是,实际上不是.

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

如果有一种我尚未抓住的方法,请通知我.

推荐答案

自2.5以来,log4j支持A 在每个翻车上都执行.

您可以控制哪些文件由以下组合删除:

  1. name(匹配 glob 或a 正则)
  2. age ("如果14天或以上删除")
  3. count("仅保留最新3")
  4. size("仅保留最新文件最高为500MB")

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

请查看 documentation "> documentation 三个可能有用的完整示例.

对于您的问题,此片段应该有效:

<RollingFile name="rollingFile" 
      fileName="/path/app.log"
      filePattern="/path/app.%d{yyyy-MM-dd}.log"
      ignoreExceptions="false">
. . .
      <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>
 . . .

<RollingFile>

最后,小心!没有办法以这种方式删除文件. : - )

其他推荐答案

您可以在此log4j的此JIRA条目中找到更多背景信息:

/a>

当您仅使用TimeBasedTriggeringPolicy

时,自动删除旧日志文件似乎是不起
作用的情况

本文地址:https://www.itbaoku.cn/post/1574800.html

问题描述

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

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>

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

But, actually, it doesn't.

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.

推荐答案

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

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:

<RollingFile name="rollingFile" 
      fileName="/path/app.log"
      filePattern="/path/app.%d{yyyy-MM-dd}.log"
      ignoreExceptions="false">
. . .
      <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>
 . . .

<RollingFile>

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

其他推荐答案

You can find more background information in this JIRA entry for log4j:

https://issues.apache.org/jira/browse/LOG4J2-524

It seems to be the case that auto deleting old log files does not work when you only use a TimeBasedTriggeringPolicy