【发布时间】:2016-07-21 20:47:42
【问题描述】:
我正在尝试将 log4j2 配置为使用 rollingFileAppender,但它没有像我预期的那样工作。
这是我当前用于配置 log4j2 的代码,它是对此处的代码稍作修改的版本:rollingFileAppender 示例下的https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator(我也使用了此处给出的示例,没有进行修改,结果相似)
LoggerContext ctx;
public Logger logger;
ConfigurationBuilder< BuiltConfiguration > builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ALL);
builder.setConfigurationName("RollingBuilder");
// create the console appender
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target",
ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
builder.add( appenderBuilder );
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
.addAttribute("pattern", "%d [%t] %-5level: %msg%n");
ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 * * * * ?"))
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
ComponentBuilder defaultStrat = builder.newComponent("DefaultRolloverStrategy")
.addAttribute("min", 10)
.addAttribute("max", 20);
appenderBuilder = builder.newAppender("rolling", "RollingFile")
.addAttribute("fileName", "targetTest/rollingTEST.log")
.addAttribute("filePattern", "targetTest/archive/rollingTEST-%d{yy-dd-MM-kk-mm}-test.log.gz")
.addAttribute("immediateFlush", false)
.addComponent(defaultStrat)
.add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(appenderBuilder);
// create the new logger
builder.add( builder.newLogger( "TestLogger", Level.ALL )
.add( builder.newAppenderRef( "rolling" ) ) );
//.addAttribute( "additivity", false ) );
builder.add( builder.newRootLogger( Level.ALL )
.add( builder.newAppenderRef( "rolling" ) ) );
this.ctx = Configurator.initialize(builder.build());
this.logger = ctx.getLogger("TestLogger");
我希望这样做:登录到 rollingTest.log 文件,然后每分钟将其归档到 rollingTest-16-21-07-11-30.log.gz 文件中,然后再次开始在 rollingTest.log 文件中记录,直到另一分钟后发生翻转那个。
实际发生的情况:日志被记录到rollingTest.log,在启动我的服务器一分钟后,第一次翻转尝试发生,但文件被删除,然后每秒翻转一次。此时如果我尝试记录更多内容,它会尝试将其归档到一个新文件中,但该新文件也会被删除。
这是发生这种情况的日志:
2016-07-21 15:20:00,009 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 6.0 milliseconds
2016-07-21 15:20:00,030 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:00,042 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:00,042 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:01,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:01,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-19-test.log, renameEmptyFiles=false]
2016-07-21 15:20:02,004 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:02,004 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:02,005 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:02,006 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:03,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:03,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:04,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:04,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:05,006 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:05,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:06,005 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:06,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:06,007 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:06,007 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:07,001 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:08,003 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:08,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:08,005 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:08,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:09,007 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:09,009 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:09,009 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:09,010 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:10,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:10,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:11,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:11,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:11,006 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:11,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:12,006 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:12,006 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:12,007 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:12,007 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:13,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:13,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
如果我将%i 参数放入文件名,那么我的新文件将不会被删除,但在初始文件完成后,翻转过程仍然会每秒触发一次,并且我最终会为每个日志事件创建一个新文件我发送给记录器。
编辑:我只是使用 XML 配置文件而不是上面的编程方法进行了尝试,结果相似。这是我使用的配置文件:
<Configuration status="DEBUG">
<Appenders>
<RollingFile name="RollingFile" fileName="target/logfile.log"
filePattern="target/archive/logfile-%d{yyyy-MM-dd-hh-mm-ss}.log" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 * * * * ?"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
</Logger>
<Logger name="TestLogger" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
当我在文件名中有%i 时,翻转从我的服务器启动的那一刻起每秒触发一次,并为发送给它的每个日志事件创建一个新文件。
当我在文件名中没有%i 时,我有两个经历:
第一个是初始事件后每秒翻转一次,每个日志事件都有一个新文件
第二个是我上面描述的,我的滚动文件在创建后被删除。这也会在初始事件后每秒触发一次翻转。
【问题讨论】:
-
您能指定您使用的是哪个 Log4j 2 版本吗?
-
我使用的是 2.6.2 版