Spring boot log4j2.xml example

Spring boot’s default logging uses Logback which is included as transitive dependency. Spring Boot supports Log4j2 for logging configuration if it is on the classpath.

To configure Log4j2 with Spring Boot2, follow these two simple steps:

1. Log4j2 Maven Dependency

In fact, it is two steps change. First remove spring’s default logging dependency and then add log4j2 dependency.

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.2.RELEASE</version>
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-logging</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>
</dependencies>

2. Add log4j2.xml file in resources folder

Spring will automatically configure log4j2 once it finds it’s jar files in classpath. Now you have to add log4j2.xml or (log4j2.properties) in src/main/resources folder. That’s all.

<?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>
		<Property name="APP_LOG_ROOT">c:/temp</Property>
	</Properties>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT" follow="true">
			<PatternLayout pattern="${LOG_PATTERN}" />
		</Console>

		<RollingFile name="appLog"
			fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
			filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%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" additivity="false">
			<AppenderRef ref="appLog" />
			<AppenderRef ref="Console" />
		</Logger>

		<Root level="debug">
			<AppenderRef ref="Console" />
		</Root>
	</Loggers>
</Configuration>

3. Spring boot log4j2 demo

Now add few log statements in application and see the logs in configured appenders e.g. I configured console and file.

package com.howtodoinjava.app;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	private static final Logger LOGGER = LogManager.getLogger(Application.class);

	public static void main(String[] args)
    {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        
        LOGGER.info("Info level log message");
        LOGGER.debug("Debug level log message");
        LOGGER.error("Error level log message");
    }
}

Check the output.

2018-06-01T13:55:42.506+0530 INFO Info level log message
2018-06-01T13:55:42.506+0530 DEBUG Debug level log message
2018-06-01T13:55:42.506+0530 ERROR Error level log message

Let me know if you face any problem in this spring boot log4j2 example.

Happy Learning !!

Leave a Reply

7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

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