简体   繁体   English

Log4j RollingFileAppender 在使用 Log4j 1.x 桥时有奇怪的行为

[英]Log4j RollingFileAppender has odd behavior when using the Log4j 1.x bridge

While I migrate to log4j2, I have configured my Tomcat web application to use the Log4j 1.x bridge.在迁移到 log4j2 时,我已将 Tomcat web 应用程序配置为使用 Log4j 1.x 网桥。 I followed the Migration guide here: https://logging.apache.org/log4j/2.x/manual/migration.html我在这里遵循了迁移指南: https://logging.apache.org/log4j/2.x/manual/migration.html

I continue to use my existing log4j.properties file which look like this:我继续使用我现有的 log4j.properties 文件,该文件如下所示:

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/mylog.out
log4j.appender.file.MaxFileSize=10KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p %t %c - %m%n

This successfully creates my log file and rolls over at 10KB.这成功地创建了我的日志文件并以 10KB 滚动。

I am having an issues that I can't explain.我有一个我无法解释的问题。 Here are the files in my log directory:以下是我的日志目录中的文件:

-rw-r----- 1 root root 7.6K Jan 10 12:27 test.log
-rw-r----- 1 root root 11K Jan 10 12:27 test.log.1
-rw-r----- 1 root root 36K Jan 10 12:27 test.log.2
-rw-r----- 1 root root 11K Jan 10 12:27 test.log2022-01-10

I get a log file named for the current day.我得到一个以当天命名的日志文件。 I've experimented and it looks like the RollingFileAppender is also rolling at the end of each day.我已经尝试过了,看起来 RollingFileAppender 也在每天结束时滚动。 It didn't do this before using the bridge.在使用网桥之前它没有这样做。 Any idea why this is happening and how to stop it?知道为什么会发生这种情况以及如何阻止它吗?

This is a bug and you should report it.这是一个错误,您应该报告它。 The RollingFileAppenderBuilder (cf. source code ) has a filePattern containing a date instead of a number: RollingFileAppenderBuilder (参见源代码)有一个包含日期而不是数字的filePattern

        String filePattern = fileName +"%d{yyy-MM-dd}";
        TriggeringPolicy timePolicy = TimeBasedTriggeringPolicy.newBuilder().setModulate(true).build();
        SizeBasedTriggeringPolicy sizePolicy = SizeBasedTriggeringPolicy.createPolicy(maxSize);
        CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(sizePolicy, timePolicy);
        RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .setConfig(config)
                .setMax(maxBackups)
                .build();
        return new AppenderWrapper(RollingFileAppender.newBuilder()
                .setName(name)
                .setConfiguration(config)
                .setLayout(fileLayout)
                .setFilter(fileFilter)
                .setBufferedIo(bufferedIo)
                .setImmediateFlush(immediateFlush)
                .setFileName(fileName)
                .setFilePattern(filePattern)
                .setPolicy(policy)
                .setStrategy(strategy)
                .build());

and hence it behaves more like the DailyRollingFileAppender , then a Log4j 1.x RollingFileAppender .因此它的行为更像DailyRollingFileAppender ,然后是 Log4j 1.x RollingFileAppender

As a workaround you can use the older Log4j1ConfigurationFactory by setting these system properties:作为一种解决方法,您可以通过设置以下系统属性来使用较旧的Log4j1ConfigurationFactory

log4j2.configurationFactory=org.apache.log4j.config.Log4j1ConfigurationFactory
log4j2.configurationFile=classpath:log4j.properties

I find at least the missing dot before the date annoying.我发现至少在日期之前丢失的点很烦人。 I created an issue: https://issues.apache.org/jira/browse/LOG4J2-3349我创建了一个问题: https://issues.apache.org/jira/browse/LOG4J2-3349

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

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