Reputation: 221
I'm testing Log4j RollingFileAppender with log4j 2.6.2.
I want to rotate the logs every minute and so I have a log4j2.xml very similar to one example of here https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender. This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="testlog4j2" packages="">
<Properties>
<Property name="baseDir">C:/tmp/testlog4</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH-mm}.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<CronTriggeringPolicy schedule="0 0/1 * * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz" />
<IfLastModified age="60d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
And this is an app where I write a log every second.
package testlog4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class TestLog4j {
private final static Logger logger = LogManager.getLogger(TestLog4j.class);
public static void main(String[] args) {
try {
for (int i=1; i<=240; i++) {
logger.info("Hello");
Thread.sleep(1*1000);
}
} catch (Exception e) {
//e.printStackTrace();
logger.error("Excepcion general", e);
}
}
}
What happens is:
once the system rotates the log at the first minute appears continuously errors like this
2016-07-28 15:10:02,015 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file C:\tmp\testlog4\2016-07\app-2016-07-28-15-10.log.gz to C:\tmp\testlog4\2016-07\app-2016-07-28-15-10.log.gz: java.nio.file.NoSuchFileException C:\tmp\testlog4\2016-07\app-2016-07-28-15-10.log.gz -> C:\tmp\testlog4\2016-07\app-2016-07-28-15-10.log.gz
There isn't a gz for every minute
What I'm I doing wrong?
Thanks
Upvotes: 2
Views: 7622
Reputation: 221
By now It seems to work with TimeBasedTriggeringPolicy instead of CronTriggeringPolicy.
My conf looks now like this
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="testlog4j2" packages="">
<Properties>
<Property name="baseDir">C:/tmp/testlog4</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/app-%d{yyyy-MM-dd-HH-mm}.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<!--
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
-->
<!--
<CronTriggeringPolicy schedule="0 0/1 * * * ?"/>
-->
<TimeBasedTriggeringPolicy interval="1"/><!-- como el filePattern tiene como unidad minima el minuto, se hara cada 1 minutos -->
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="0">
<IfFileName glob="*/app-*.log" />
<IfLastModified age="2M" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
The delete is not working by now but that's another story I will investigate next.
Best regards
Upvotes: 0
Reputation: 36844
You may have found a bug. Please raise a bug report on the Jira issue tracker with all the details you describe here.
Upvotes: 1