I am trying to create new log files on an hourly basis. I am using TimeBasedTriggerringPolicy of lo4j2 in RollingFileAppender. Below is the sample xml configuration code i have taken from log4j2 official site.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
**
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
**
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
In the interval attribute I have set 1 which signifies 1 hour. But still my file does not roll every 1 hour.
Please help me find any mistake.
Note : I have included beta9 of log4j2 (which is the latest)
Log4j documentations:
You should change the filename pattern if you would like to create it every hour.
1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.
For manual testing, I change the system date and time. First, try with increasing 1 hour. The log files will be generated but not as per the expectation. Then change the system date, increase by 1 day and then see the results.
Suppose the last log file (abc.log) on day 29-Oct is of 50 KB. Configuration size is 100 KB. If we change the day (increase by 1 day) and then run. Then, last file will be renamed 29-Oct-(some sequence number).log (50 KB file as it is copied) and new file will be created with abc.log
I have tried this with simple servlet with below configuration in web.xml
keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.
You do have a non-empty log file (otherwise there is nothing to roll over)?
Note that even though the name is "TimeBased..." It will not actually roll over at the specified time, but at the first log event that arrives after the time threshold has been exceeded. Can you try with a small test program that logs something after 61 minutes or so and see if the problem still occurs?
If it doesn't roll over with the above test program, you may have found a bug. In that case please raise it on the log4j issue tracker. (Be sure to attach the test program the team can use to reproduce the issue).
As Abid mentioned, interval value is interpreted in context of pattern that is specified as part of filePattern. It starts with lowest denomination. For example,if pattern contains S, frequency will be in millisecond. It supports the date pattern as described in detail as part of SimpleDateFormat java doc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html