Given log4j2.xml is reference to configure multiple appenders such as console appender and file appenders. This also configure the dynamic log root path.
Log4j2 Multiple Appenders – XML Configuration
Sample log4j2 configuration is given blow. It does following things:
- Uses dynamic log root path where log files will be created. Pass environment variable as
-DAPP_LOG_ROOT=c:/tempto configure it. - Demo the usage of property constants defined in config file e.g.
LOG_PATTERNin below file. - It uses
LevelRangeFilterto log different log level statements in different files i.e. debug logs in one file and error logs in separate file. - All logs will be shown in console also.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="debugLog" fileName="${sys:APP_LOG_ROOT}/application-debug.log"
filePattern="${sys:APP_LOG_ROOT}/application-debug-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${sys:APP_LOG_ROOT}/application-info.log"
filePattern="${sys:APP_LOG_ROOT}/application-info-%d{yyyy-MM-dd}-%i.log" >
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="errorLog" fileName="${sys:APP_LOG_ROOT}/application-error.log"
filePattern="${sys:APP_LOG_ROOT}/application-error-%d{yyyy-MM-dd}-%i.log" >
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="springLog" fileName="${sys:APP_LOG_ROOT}/spring.log"
filePattern="${sys:APP_LOG_ROOT}/spring-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1"/>
</RollingFile>
<RollingFile name="aopLog" fileName="${sys:APP_LOG_ROOT}/application-aop.log"
filePattern="${sys:APP_LOG_ROOT}/application-aop-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.howtodoinjava.app.aop" additivity="false">
<AppenderRef ref="aopLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="com.howtodoinjava.app" additivity="false">
<AppenderRef ref="debugLog" />
<AppenderRef ref="infoLog" />
<AppenderRef ref="errorLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.springframework" additivity="false">
<AppenderRef ref="springLog" />
<AppenderRef ref="Console"/>
</Logger>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Drop me your questions in the comments section.
Happy Learning !!
Comments