Custom Log4j Configuration Examples
You can create your own custom log4j configurations that you can use for logging instead of the default log4j2.xml file.
You can also see the official log4j wiki page for more information on log4j configurations at the Apache Log4j 2 webpage.
Following are the few example XML configurations for some logging use cases, which you can use after overriding the default logging (see Overriding the Default Logging Mode for more information).
Multiple Log Files Using Single log4j Configuration
Define multiple FileAppender classes to create multiple logging by adding appender reference to the respective loggers as shown in the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy MMM dd HH:mm:ss.SSS 'GMT'XXX} %p [%c{3}] - %m%n" charset="UTF-8"/>
</Console>
<RollingFile name="RollingFile1" fileName="${LOG_DIR}/application.log" filePattern="${LOG_DIR}/application.%d{yyyy-MM-dd}.log.gz" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100KB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<RollingFile name="RollingFile2" fileName="${LOG_DIR}/action.log" filePattern="${LOG_DIR}/action.%d{dd-MMM}.log.gz" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100KB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.tibco.rta.action" level="info" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingFile1"/>
<AppenderRef ref="RollingFile2"/>
</Logger>
<Logger name="org.apache.catalina" level="info" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingFile1"/>
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingFile1"/>
<AppenderRef ref="RollingFile2"/>
</Root>
</Loggers>
</Configuration>
Rotating Log Files Based on Time
If using RollingFileAppender, then use TimeBasedRollingPolicy to specify when to roll over log files based on date time. The FileNamePattern property defines the name pattern for rolled over files. In the given example, it creates daily a log file (max 5 files).
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/myapp.log" filePattern="logs/myapp-%d{yyyy-MM-dd}.log">
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="logs" maxDepth="1">
<IfFileName glob="myapp-*.log">
<IfAny>
<IfAccumulatedFileCount exceeds="5"/>
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger additivity="true" level="debug" name="kernel.core">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
The following table displays the example values of the DatePattern parameter that defines the time when to roll over the logs.
| Time | Value |
|---|---|
| Minutely | .%d{yyyy-MM-dd-HH-mm} |
| Hourly | .%d{yyyy-MM-dd-HH} |
| Half-daily | .%d{yyyy-MM-dd-a} |
| Daily | .%d{yyyy-MM-dd} |
| Weekly | .%d{yyyy-ww} |
| Monthly | .%d{yyyy-MM} |