简体   繁体   English

使用 log4j2 2.5 归档日志文件

[英]Archiving log file using log4j2 2.5

I have been trying to archive my application logs file which are older than a certain period.我一直在尝试归档我的应用程序日志文件,这些文件早于某个时期。 Noticed that since log4j 2.5 we have a Delete tag which let's you define the criteria based on which we can delete/archive our logs.请注意,从 log4j 2.5 开始,我们有一个Delete标签,它让您可以定义我们可以删除/归档日志的标准。 Tried using this but I am somehow not able to crack it.尝试使用它,但我不知何故无法破解它。 Tried with a 30day 30d value and that isn't working on my server and neither is a 20 second policy PT20S working in my Dev Machine.尝试使用 30day 30d值,但它在我的服务器上不起作用,而且 20 秒策略PT20S在我的开发机器上也不起作用。

Any direction is greatly appreciated.任何方向都非常感谢。

XML is as below: XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
    <RollingFile name="RollingFile" fileName="C://logs///test.log" filePattern="C://logs//test-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
      <PatternLayout pattern="%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
      <TimeBasedTriggeringPolicy />
      <DefaultRolloverStrategy>
        <Delete basePath="C://logs//" maxDepth="2">
          <IfFileName glob="*/test-*.log.gz" />
          <IfLastModified age="PT20S" />
        </Delete>
      </DefaultRolloverStrategy>
    </RollingFile>
   </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Console" />
      <AppenderRef ref="RollingFile" />
    </Root>
  </Loggers>
</Configuration>

If you're using Windows the file and filePattern of the RollingFile appender can use single slashes.如果您使用的是 Windows,则 RollingFile 附加程序的 file 和 filePattern 可以使用单斜杠。 The double slashes may confuse it.双斜线可能会混淆它。

You can debug by setting <Configuration status="trace"> in the beginning of the configuration file.您可以通过在配置文件的开头设置<Configuration status="trace">进行调试。


Update:更新:

The rolled over files end up in the c:/logs directory ( filePattern="C://logs//test-%d{MM-dd-yyyy}.log.gz" ).翻转的文件最终位于 c:/logs 目录( filePattern="C://logs//test-%d{MM-dd-yyyy}.log.gz" )。

However, the Delete action is configured to only look at files ending in "log.gz" that are in subdirectories of c:/logs.但是,删除操作配置为仅查看 c:/logs子目录中以“log.gz”结尾的文件。 Files in the c:/logs directory itself are not matched by glob="*/test-*.log.gz" . c:/logs 目录本身中的文件与glob="*/test-*.log.gz"不匹配。

To fix this, use glob="test-*.log.gz" .要解决此问题,请使用glob="test-*.log.gz" It was mentioned in the comments that changing glob to regex also resolved the problem.评论中提到将 glob 更改为 regex 也解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM