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)
When Log4j initializes it will calculate the rollover interval based on the %d pattern. When modulate is false then the rollover will happen based on the time the application started. So if the application started at 1:41 pm and the smallest time interval is the hour then the next rollover will occur at 2:41 pm.
Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.
The DefaultRolloverStrategy is a combination of a time-based policy and a fixed-window policy. When the file name pattern contains a date format then the rollover time interval will be used to calculate the time to use in the file pattern.
Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.
1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.
<RollingFile name="T" fileName="/data_test/log/abc.log" filePattern="/data_test/log/abc-%d{MM-dd-yyyy}-%i.log"> <PatternLayout> <Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> <SizeBasedTriggeringPolicy size="100 KB" /> </Policies> </RollingFile>
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
<context-param> <param-name>log4jConfiguration</param-name> <param-value>log4j2.xml</param-value> </context-param>
keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.
Log4j documentations:
interval -> (integer) How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers would occur every 4 hours. The default value is 1.
You should change the filename pattern if you would like to create it every hour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With