xml log4j2 - 限制日志文件的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13502722/
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-09-06 13:47:29  来源:igfitidea点击:

log4j2 - limiting the number of log files

xmlloggingconfigurationlog4jlog4j2

提问by Christopher Schmidt

I have the following log4j2.xml file:

我有以下 log4j2.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
  <appenders>
    <RollingFile name="testLog" fileName="test.log" filePattern="" append="false">
      <PatternLayout pattern="[%t] %-5level - %msg%n%n"/>
    <SizeBasedTriggeringPolicy size="5mb" />
    </RollingFile>
  </appenders>
  <loggers>
    <logger name="TestsLogger" level="trace" additivity="false">
      <appender-ref ref="testLog"/>
    </logger>
    <root level="debug">
      <appender-ref ref="testLog"/>
    </root>
  </loggers>
</configuration>

How can I modify this configuration such that

如何修改此配置,以便

  1. Instead of overwriting the same logfile over and over again a new file is created after the 5mb limit has been reached. It would be nice to have something like test1.log, test2.log and so on.
  2. How can I limit the number of partial log files created in 1.? What I want to achieve is a scheme like the following:

    creating test1.log [present log files: test1.log]
    test1.log - 5mb limit reached
    creating test2.log [present log files: test1.log, test2.log]
    test2.log - 5mb limit reached
    creating test3.log [present log files: test2.log, test3.log]
    test3.log - 5mb limit reached
    creating test4.log [present log files: test3.log, test4.log]
    and so on
    
  1. 在达到 5mb 限制后,将创建一个新文件,而不是一遍又一遍地覆盖相同的日志文件。有类似 test1.log、test2.log 之类的东西会很好。
  2. 如何限制在 1. 中创建的部分日志文件的数量?我想要实现的是如下方案:

    creating test1.log [present log files: test1.log]
    test1.log - 5mb limit reached
    creating test2.log [present log files: test1.log, test2.log]
    test2.log - 5mb limit reached
    creating test3.log [present log files: test2.log, test3.log]
    test3.log - 5mb limit reached
    creating test4.log [present log files: test3.log, test4.log]
    and so on
    

Does anyone know, how to achieve something like that? Of course it would be nice, if something like that was possible with log4j2 alone. But maybe there is a way to combine log4j2 with some kind of external program that would run alongside the main Java application and delete superfluous log files, while keeping the two last log files intact. So if anyone has at least a suggestion for 1., it may be already, what I'm looking for. Because I may be able to write a program for the 2nd part. Of course it would be awesome, if the 2nd part could be done with log4j2 as well.

有谁知道,如何实现这样的目标?当然,如果单独使用 log4j2 就可以实现这样的事情,那就太好了。但也许有一种方法可以将 log4j2 与某种外部程序结合起来,这些程序将与主 Java 应用程序一起运行并删除多余的日志文件,同时保持最后两个日志文件的完整性。因此,如果有人至少对 1. 有建议,那么它可能已经是我正在寻找的。因为我也许可以为第二部分编写程序。当然,如果第二部分也可以用 log4j2 完成,那就太棒了。

回答by FrVaBe

I never used log4j2 so far but the documentation of the RollingFileAppendergives you a lot of configuration examples.

到目前为止,我从未使用过 log4j2,但RollingFileAppender的文档为您提供了很多配置示例。

Interesting for you seams to be something like this (using DefaultRolloverStrategy):

有趣的是你的接缝是这样的(使用DefaultRolloverStrategy):

<RollingFile name="RollingFile" fileName="logs/app.log"
             filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
  <PatternLayout>
    <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  </PatternLayout>
  <Policies>
    <SizeBasedTriggeringPolicy size="5 MB"/>
  </Policies>
  <DefaultRolloverStrategy max="20"/>
</RollingFile>