Spring Boot Log4j2.xml Example

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

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 spring-boot-starter-logging starter dependency which excludes Logback from the classpath. Then add the spring-boot-starter-log4j2 starter dependency.

 <dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</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 its jar files in the classpath. Now we 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" />
            <AppenderRef ref="appLog" />
        </Root>
    </Loggers>
</Configuration>

3. Demo

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

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 in the console and the log file.

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 problems in this spring boot log4j2 example.

Happy Learning !!

Comments

Subscribe
Notify of
guest
14 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

Dark Mode

Dark Mode