Log4j2 XML Configuration: Log4j2.xml Example

Learn to configure log4j2.xml file to output the log statements to the console, rolling files etc. Learn to configure log4j2 appenders, levels and patterns.

Apache Log4j2 is an upgrade to Log4j 1.x that provides significant improvements over its predecessor such as performance improvement, automatic reloading of modified configuration files, Java 8 lambda support and custom log levels.

If not upgraded already, it is highly recommended to upgrade Log4j2 to the latest version 2.16.0 which has the fix for a recently found vulnerability CVE-2021-45046. Read the latest updates to the library here.

1. Log4j2 Dependencies

Find the latest version from this link. Please note that using Log4j2 with SLF4J is recommended approach.

1.1. Maven

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.20.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.20.0</version>
</dependency>

1.2 Gradle

dependencies {
    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
}

2. ConsoleAppender – Logging to Cosole

We can use below log4j2.xml file logging output into the console. It uses the ConsoleAppender API.

Please note that if no configuration file can be located while initializing the Log4j2 then DefaultConfiguration will be used. The default configuration causes logging output to go to the console.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Extra logging related to initialization of Log4j. 
 Set to debug or trace if log4j initialization is failing. -->
<Configuration status="warn">
    <Appenders>
    	<!-- Console appender configuration -->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
    	<!-- Root logger referring to console appender -->
        <Root level="info" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

3. RollingFileAppender – Logging to File

We can use the below log4j2.xml file logging output with time and size-based rolling files.

The given RollingFileAppender configuration rolls over the log every day, or when the log file size gets greater than 10 MB. It also deletes all log files older than 30 days.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
	<Properties>
		<Property name="basePath">C:/temp/logs</Property>
	</Properties>

	<Appenders>
		<RollingFile name="fileLogger"
			fileName="${basePath}/app.log"
			filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
			<PatternLayout>
				<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
				</pattern>
			</PatternLayout>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1" modulate="true" />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
			<!-- Max 10 files will be created everyday -->
			<DefaultRolloverStrategy max="10">
				<Delete basePath="${basePathr}" maxDepth="10">
					<!-- Delete all files older than 30 days -->
					<IfLastModified age="30d" />
				</Delete>
			</DefaultRolloverStrategy>
		</RollingFile>
	</Appenders>
	<Loggers>
		<Root level="info" additivity="false">
			<appender-ref ref="fileLogger" />
		</Root>
	</Loggers>
</Configuration>

4. Configuring Multiple Appenders

Use this simple log4j2.xml for quick reference to log statements in multiple log files.

This configuration logs different levels of logs (debug, info etc.) to different files, using LevelRangeFilter, so that our logs are clean and separated for easy debugging and reporting purposes.

Change the configuration for multiple appenders as per your requirements.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
 
    <!-- Logging Properties -->
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Property name="APP_LOG_ROOT">c:/temp/logs</Property>
    </Properties>
     
    <Appenders>
     
        <!-- Console Appender -->
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
         
        <!-- File Appenders on need basis -->
        <RollingFile name="frameworkLog" fileName="${APP_LOG_ROOT}/app-framework.log"
            filePattern="${APP_LOG_ROOT}/app-framework-%d{yyyy-MM-dd}-%i.log">
            <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="debugLog" fileName="${APP_LOG_ROOT}/app-debug.log"
            filePattern="${APP_LOG_ROOT}/app-debug-%d{yyyy-MM-dd}-%i.log">
            <LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="infoLog" fileName="${APP_LOG_ROOT}/app-info.log"
            filePattern="${APP_LOG_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log" >
            <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="errorLog" fileName="${APP_LOG_ROOT}/app-error.log"
            filePattern="${APP_LOG_ROOT}/app-error-%d{yyyy-MM-dd}-%i.log" >
            <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="perfLog" fileName="${APP_LOG_ROOT}/app-perf.log"
            filePattern="${APP_LOG_ROOT}/app-perf-%d{yyyy-MM-dd}-%i.log" >
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="1"/>
        </RollingFile>
         
        <RollingFile name="traceLog" fileName="${APP_LOG_ROOT}/app-trace.log"
            filePattern="${APP_LOG_ROOT}/app-trace-%d{yyyy-MM-dd}-%i.log" >
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="1"/>
        </RollingFile>
         
    </Appenders>
 
    <Loggers>
     
        <Logger name="com.howtodoinjava.app.pref" additivity="false" level="trace">
            <AppenderRef ref="traceLog" />
            <AppenderRef ref="Console" />
        </Logger>
         
        <Logger name="com.howtodoinjava.app" additivity="false" level="debug">
            <AppenderRef ref="debugLog" />
            <AppenderRef ref="infoLog"  />
            <AppenderRef ref="errorLog" />
            <AppenderRef ref="Console"  />
        </Logger>
         
        <Logger name="org.framework.package" additivity="false" level="info">
            <AppenderRef ref="perfLog" />
            <AppenderRef ref="Console"/>
        </Logger>
                 
        <Root level="warn">
            <AppenderRef ref="Console"/>
        </Root>
 
    </Loggers>
 
</Configuration>

5. The log4j2.xml File Location

We should put log4j2.xml anywhere in the application’s classpath. Log4j will scan all classpath locations to find out this file and then load it.

We can find this file mostly placed in the ‘src/main/resources‘ folder.

Log4j2.xml file location
Log4j2.xml file location

If we are using an external log4j2 configuration file, then we can provide the path of the configuration file using the application startup parameter or system property log4j.configurationFile. Note that this property value is not restricted to a location on the local file system and may contain a URL.

-Dlog4j2.configurationFile=file:/home/lokesh/log4j2.xml

A commonly seen approach is to set the log4j.configurationFile property in the method annotated with @BeforeAll in the junit test class. This will allow an arbitrarily named file to be used during the test.

6. Demo

Let’s write a java class and write a few log statements to verify that logs are appearing in the console and log file as well. It logs different log levels to different logs

6.1. Core Log4j2 API

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Main {

	private static final Logger logger = LogManager.getLogger(Main.class);

	public static void main(final String... args) 
	{
		logger.debug("Debug Message Logged !!!");
		logger.info("Info Message Logged !!!");
		logger.error("Error Message Logged !!!", new NullPointerException("NullError"));
	}
}

6.2. Log4j2 with SLF4j

As stated in the beginning, it is recommended to use Log4j with SLF4j API.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

	private static final Logger logger = LoggerFactory.getLogger(Main.class);

	public static void main(final String[] args)
	{
		logger.debug("Debug Message Logged !!!");
		logger.info("Info Message Logged !!!");
		logger.error("Error Message Logged !!!", new NullPointerException("NullError"));
	}
}

Happy Learning !!

Comments are closed for this article!

Comments

13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode