Spring Boot and log4j2 intergation Example

In our previous article we have seen how to create Spring Boot RESTful Web services, In this article we will see, How to enable Logging in Spring Boot using log4j2.

We will disable the default spring boot logging to add log4j2 support, let's first Create a spring maven project and import it in eclipse.

Dependencies (pom.xml)

In order to disable default spring boot logging we need to exclude spring-boot-starter-logging and to enable log4j2 logging a spring-boot-starter-log4j2 has to be added in pom.xml as shown below:
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">


		<!-- Exclude Spring Boot's Default Logging -->

		<!-- Add Log4j2 Dependency -->


			<name>Spring GA Repository</name>

Create log4j2.xml configuration

If no log4j2.xml configuration found in classpath, a default configuration will be applied; to add a custom log4j2 configuration add a log4j2.xml file in classpath as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        <Logger name="com.tb.java" level="debug" additivity="false">
            <AppenderRef ref="ConsoleAppender" />

        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />

Create application.properties

In order to tell spring boot to consider log4j2.xml file for logging behaviour we need to add an entry with full path in application.properties, as shown below:


Create Spring boot application launch class

Here, @SpringBootApplication does all the magic, a class having main() and annotated with @SpringBootApplication is called Spring boot application launch class.

package com.tb.java;

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

 * Hello world!
public class App {
	private static final Logger LOGGER = LogManager.getLogger(App.class);

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(App.class, args);
		LOGGER.debug("A DEBUG Message");
		LOGGER.info("An INFO Message");
		LOGGER.warn("A WARN Message");
		LOGGER.error("An ERROR Message");

Run and launch application

Now we are all done with creating a Spring Boot project with log4j2 logging enabled, lets run the application from application launch class to see log messages:

If everything goes right, you will see something like this in Eclipse console, along with log messages printed in the end:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-01 14:52:26.566  INFO techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : Starting App on techie-ThinkPad-13-2nd-Gen with PID 17028 (/home/techie/git/SpringBootLg4j/target/classes started by techie in /home/techie/git/SpringBootLg4j)
2019-03-01 14:52:26.571 DEBUG techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : Running with Spring Boot v2.1.3.RELEASE, Spring v5.1.5.RELEASE
2019-03-01 14:52:26.572  INFO techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : No active profile set, falling back to default profiles: default
2019-03-01 14:52:26.992  INFO techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : Started App in 0.656 seconds (JVM running for 1.29)

2019-03-01 14:52:26.993 DEBUG techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : A DEBUG Message
2019-03-01 14:52:26.994  INFO techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : An INFO Message
2019-03-01 14:52:26.994  WARN techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : A WARN Message
2019-03-01 14:52:26.994 ERROR techie-ThinkPad-13-2nd-Gen --- [           main] c.t.j.App                                : An ERROR Message

Here We are done with creating a Spring Boot 2 project with log4j2 logging enabled, in upcoming articles we will see more about Spring Boot.

Download "Spring Boot 2 log4j2 project" source code from: GitHub.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.