Rolling based on size of file
You can use the SizeBasedTriggeringPolicy to rollover the log file based on the size of file as follows.
<RollingFile name="RollingFile">
<FileName>C:/log/mylog.logFileName>
<FilePattern>C:/log/size-based-logs/%d{yyyy-MM-dd-hh}-%i.log.zipFilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%nPattern>
PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 KB"/>
Policies>
<DefaultRolloverStrategy max="5" />
RollingFile>
You can specify the size of file in bytes, with the suffix KB, MB or GB.
Here is the complete log4j2.xml
file for rolling files based on the specified size in
element.
log4j2.xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
Console>
<RollingFile name="RollingFile">
<FileName>C:/log/mylog.logFileName>
<FilePattern>C:/log/size-based-logs/%d{yyyy-MM-dd-hh}-%i.log.zipFilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%nPattern>
PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 KB" />
Policies>
<DefaultRolloverStrategy max="5" />
RollingFile>
Appenders>
<Loggers>
<Logger name="com.boraji.tutorial.log4j2" level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
Logger>
<Root level="trace">
<AppenderRef ref="Console" />
Root>
Loggers>
Configuration>
The following is a simple java program to test the above log4j2.xml configuration.
package com.boraji.tutorial.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MainApp {
private static final Logger logger = LogManager.getLogger(MainApp.class);
public static void main(String[] args) {
for (int i = 0; i < 50000; i++) {
logger.info("Rolling file appender example...");
}
}
}
Rolling based on cron expression
You can use the CronTriggeringPolicy to rollover the log file based on the specified cron expression as follows.
<RollingFile name="RollingFile">
<FileName>C:/log/mylog.logFileName>
<FilePattern>C:/log/cron-based-logs/%d{yyyy-MM-dd-hh-mm}-%i.log.zipFilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%nPattern>
PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0/2 * 1/1 * ? *" />
Policies>
<DefaultRolloverStrategy max="5" />
RollingFile>
The following is the complete log4j2.xml
file, which trigger rollover after every 2nd minutes as specified in schedule
attribute of
element by cron expression.
log4j2.xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
Console>
<RollingFile name="RollingFile">
<FileName>C:/log/mylog.logFileName>
<FilePattern>C:/log/cron-based-logs/%d{yyyy-MM-dd-hh-mm}-%i.log.zipFilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%nPattern>
PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0/2 * 1/1 * ? *" />
Policies>
<DefaultRolloverStrategy max="5" />
RollingFile>
Appenders>
<Loggers>
<Logger name="com.boraji.tutorial.log4j2" level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
Logger>
<Root level="trace">
<AppenderRef ref="Console" />
Root>
Loggers>
Configuration>
The following is a simple java program to test the above log4j2.xml configuration.
package com.boraji.tutorial.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MainApp {
private static final Logger logger = LogManager.getLogger(MainApp.class);
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
logger.info("Rolling file appender example...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
https://www.boraji.com/log4j-2-rollingfileappender-example